Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Gravis UltraSound Classic soundcard |
| 3 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> |
| 4 | * |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <sound/driver.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 24 | #include <linux/err.h> |
| 25 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/delay.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/moduleparam.h> |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 29 | #include <asm/dma.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <sound/core.h> |
| 31 | #include <sound/gus.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #define SNDRV_LEGACY_FIND_FREE_IRQ |
| 33 | #define SNDRV_LEGACY_FIND_FREE_DMA |
| 34 | #include <sound/initval.h> |
| 35 | |
| 36 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); |
| 37 | MODULE_DESCRIPTION("Gravis UltraSound Classic"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound Classic}}"); |
| 40 | |
| 41 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 42 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 43 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ |
| 44 | static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */ |
| 45 | static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,5,9,11,12,15 */ |
| 46 | static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ |
| 47 | static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ |
| 48 | static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; |
| 49 | /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ |
| 50 | static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; |
| 51 | static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; |
| 52 | |
| 53 | module_param_array(index, int, NULL, 0444); |
| 54 | MODULE_PARM_DESC(index, "Index value for GUS Classic soundcard."); |
| 55 | module_param_array(id, charp, NULL, 0444); |
| 56 | MODULE_PARM_DESC(id, "ID string for GUS Classic soundcard."); |
| 57 | module_param_array(enable, bool, NULL, 0444); |
| 58 | MODULE_PARM_DESC(enable, "Enable GUS Classic soundcard."); |
| 59 | module_param_array(port, long, NULL, 0444); |
| 60 | MODULE_PARM_DESC(port, "Port # for GUS Classic driver."); |
| 61 | module_param_array(irq, int, NULL, 0444); |
| 62 | MODULE_PARM_DESC(irq, "IRQ # for GUS Classic driver."); |
| 63 | module_param_array(dma1, int, NULL, 0444); |
| 64 | MODULE_PARM_DESC(dma1, "DMA1 # for GUS Classic driver."); |
| 65 | module_param_array(dma2, int, NULL, 0444); |
| 66 | MODULE_PARM_DESC(dma2, "DMA2 # for GUS Classic driver."); |
| 67 | module_param_array(joystick_dac, int, NULL, 0444); |
| 68 | MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS Classic driver."); |
| 69 | module_param_array(channels, int, NULL, 0444); |
| 70 | MODULE_PARM_DESC(channels, "GF1 channels for GUS Classic driver."); |
| 71 | module_param_array(pcm_channels, int, NULL, 0444); |
| 72 | MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS Classic driver."); |
| 73 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 74 | static struct platform_device *devices[SNDRV_CARDS]; |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 77 | #define PFX "gusclassic: " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Takashi Iwai | 5e2da20 | 2005-11-17 14:36:44 +0100 | [diff] [blame] | 79 | static int __init snd_gusclassic_detect(struct snd_gus_card * gus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 81 | unsigned char d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 83 | snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ |
| 84 | if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) { |
| 85 | snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return -ENODEV; |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | udelay(160); |
| 89 | snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ |
| 90 | udelay(160); |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 91 | if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) { |
| 92 | snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return -ENODEV; |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Takashi Iwai | 5e2da20 | 2005-11-17 14:36:44 +0100 | [diff] [blame] | 98 | static void __init snd_gusclassic_init(int dev, struct snd_gus_card * gus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | gus->equal_irq = 0; |
| 101 | gus->codec_flag = 0; |
| 102 | gus->max_flag = 0; |
| 103 | gus->joystick_dac = joystick_dac[dev]; |
| 104 | } |
| 105 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 106 | static int __init snd_gusclassic_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 108 | int dev = pdev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1}; |
| 110 | static int possible_dmas[] = {5, 6, 7, 1, 3, -1}; |
| 111 | int xirq, xdma1, xdma2; |
Takashi Iwai | 5e2da20 | 2005-11-17 14:36:44 +0100 | [diff] [blame] | 112 | struct snd_card *card; |
Takashi Iwai | 5e2da20 | 2005-11-17 14:36:44 +0100 | [diff] [blame] | 113 | struct snd_gus_card *gus = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | int err; |
| 115 | |
| 116 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0); |
| 117 | if (card == NULL) |
| 118 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (pcm_channels[dev] < 2) |
| 120 | pcm_channels[dev] = 2; |
| 121 | |
| 122 | xirq = irq[dev]; |
| 123 | if (xirq == SNDRV_AUTO_IRQ) { |
| 124 | if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) { |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 125 | snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); |
| 126 | err = -EBUSY; |
| 127 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | xdma1 = dma1[dev]; |
| 131 | if (xdma1 == SNDRV_AUTO_DMA) { |
| 132 | if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) { |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 133 | snd_printk(KERN_ERR PFX "unable to find a free DMA1\n"); |
| 134 | err = -EBUSY; |
| 135 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | xdma2 = dma2[dev]; |
| 139 | if (xdma2 == SNDRV_AUTO_DMA) { |
| 140 | if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) { |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 141 | snd_printk(KERN_ERR PFX "unable to find a free DMA2\n"); |
| 142 | err = -EBUSY; |
| 143 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 147 | if (port[dev] != SNDRV_AUTO_PORT) { |
| 148 | err = snd_gus_create(card, |
| 149 | port[dev], |
| 150 | xirq, xdma1, xdma2, |
| 151 | 0, channels[dev], pcm_channels[dev], |
| 152 | 0, &gus); |
| 153 | } else { |
| 154 | /* auto-probe legacy ports */ |
| 155 | static unsigned long possible_ports[] = { |
| 156 | 0x220, 0x230, 0x240, 0x250, 0x260, |
| 157 | }; |
| 158 | int i; |
| 159 | for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { |
| 160 | err = snd_gus_create(card, |
| 161 | possible_ports[i], |
| 162 | xirq, xdma1, xdma2, |
| 163 | 0, channels[dev], pcm_channels[dev], |
| 164 | 0, &gus); |
| 165 | if (err >= 0) { |
| 166 | port[dev] = possible_ports[i]; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if (err < 0) |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 172 | goto _err; |
| 173 | |
| 174 | if ((err = snd_gusclassic_detect(gus)) < 0) |
| 175 | goto _err; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | snd_gusclassic_init(dev, gus); |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 178 | if ((err = snd_gus_initialize(gus)) < 0) |
| 179 | goto _err; |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (gus->max_flag || gus->ess_flag) { |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 182 | snd_printk(KERN_ERR PFX "GUS Classic or ACE soundcard was not detected at 0x%lx\n", gus->gf1.port); |
| 183 | err = -ENODEV; |
| 184 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 186 | |
| 187 | if ((err = snd_gf1_new_mixer(gus)) < 0) |
| 188 | goto _err; |
| 189 | |
| 190 | if ((err = snd_gf1_pcm_new(gus, 0, 0, NULL)) < 0) |
| 191 | goto _err; |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!gus->ace_flag) { |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 194 | if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0) |
| 195 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %d, dma %d", gus->gf1.port, xirq, xdma1); |
Alexey Dobriyan | 1d79716 | 2006-01-25 14:30:44 +0100 | [diff] [blame] | 198 | if (xdma2 >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | sprintf(card->longname + strlen(card->longname), "&%d", xdma2); |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 200 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 201 | snd_card_set_dev(card, &pdev->dev); |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 202 | |
| 203 | if ((err = snd_card_register(card)) < 0) |
| 204 | goto _err; |
| 205 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 206 | platform_set_drvdata(pdev, card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
Takashi Iwai | 43bcd97 | 2005-09-05 17:19:20 +0200 | [diff] [blame] | 208 | |
| 209 | _err: |
| 210 | snd_card_free(card); |
| 211 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 214 | static int snd_gusclassic_remove(struct platform_device *devptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 216 | snd_card_free(platform_get_drvdata(devptr)); |
| 217 | platform_set_drvdata(devptr, NULL); |
| 218 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 221 | #define GUSCLASSIC_DRIVER "snd_gusclassic" |
| 222 | |
| 223 | static struct platform_driver snd_gusclassic_driver = { |
| 224 | .probe = snd_gusclassic_probe, |
| 225 | .remove = snd_gusclassic_remove, |
| 226 | /* FIXME: suspend/resume */ |
| 227 | .driver = { |
| 228 | .name = GUSCLASSIC_DRIVER |
| 229 | }, |
| 230 | }; |
| 231 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 232 | static void __init_or_module snd_gusclassic_unregister_all(void) |
| 233 | { |
| 234 | int i; |
| 235 | |
| 236 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
| 237 | platform_device_unregister(devices[i]); |
| 238 | platform_driver_unregister(&snd_gusclassic_driver); |
| 239 | } |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | static int __init alsa_card_gusclassic_init(void) |
| 242 | { |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 243 | int i, cards, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 245 | err = platform_driver_register(&snd_gusclassic_driver); |
| 246 | if (err < 0) |
| 247 | return err; |
| 248 | |
| 249 | cards = 0; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 250 | for (i = 0; i < SNDRV_CARDS; i++) { |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 251 | struct platform_device *device; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 252 | if (! enable[i]) |
| 253 | continue; |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 254 | device = platform_device_register_simple(GUSCLASSIC_DRIVER, |
| 255 | i, NULL, 0); |
Rene Herman | d0ac642 | 2006-04-11 14:08:33 +0200 | [diff] [blame] | 256 | if (IS_ERR(device)) |
| 257 | continue; |
Rene Herman | dcccdd9 | 2006-04-11 14:09:37 +0200 | [diff] [blame] | 258 | if (!platform_get_drvdata(device)) { |
| 259 | platform_device_unregister(device); |
| 260 | continue; |
| 261 | } |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 262 | devices[i] = device; |
Takashi Iwai | 654aa66 | 2005-11-17 17:13:43 +0100 | [diff] [blame] | 263 | cards++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | if (!cards) { |
| 266 | #ifdef MODULE |
| 267 | printk(KERN_ERR "GUS Classic soundcard not found or device busy\n"); |
| 268 | #endif |
Rene Herman | d0ac642 | 2006-04-11 14:08:33 +0200 | [diff] [blame] | 269 | snd_gusclassic_unregister_all(); |
| 270 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static void __exit alsa_card_gusclassic_exit(void) |
| 276 | { |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 277 | snd_gusclassic_unregister_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | module_init(alsa_card_gusclassic_init) |
| 281 | module_exit(alsa_card_gusclassic_exit) |