Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for generic MPU-401 boards (UART mode only) |
| 3 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> |
| 4 | * Copyright (c) 2004 by Castet Matthieu <castet.matthieu@free.fr> |
| 5 | * |
| 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 <sound/driver.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/pnp.h> |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
| 29 | #include <sound/core.h> |
| 30 | #include <sound/mpu401.h> |
| 31 | #include <sound/initval.h> |
| 32 | |
| 33 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); |
| 34 | MODULE_DESCRIPTION("MPU-401 UART"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
| 37 | static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */ |
| 38 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 39 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ |
| 40 | #ifdef CONFIG_PNP |
| 41 | static int pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; |
| 42 | #endif |
| 43 | static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */ |
| 44 | static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */ |
| 45 | |
| 46 | module_param_array(index, int, NULL, 0444); |
| 47 | MODULE_PARM_DESC(index, "Index value for MPU-401 device."); |
| 48 | module_param_array(id, charp, NULL, 0444); |
| 49 | MODULE_PARM_DESC(id, "ID string for MPU-401 device."); |
| 50 | module_param_array(enable, bool, NULL, 0444); |
| 51 | MODULE_PARM_DESC(enable, "Enable MPU-401 device."); |
| 52 | #ifdef CONFIG_PNP |
| 53 | module_param_array(pnp, bool, NULL, 0444); |
| 54 | MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device."); |
| 55 | #endif |
| 56 | module_param_array(port, long, NULL, 0444); |
| 57 | MODULE_PARM_DESC(port, "Port # for MPU-401 device."); |
| 58 | module_param_array(irq, int, NULL, 0444); |
| 59 | MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device."); |
| 60 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 61 | static struct platform_device *platform_devices[SNDRV_CARDS]; |
Bjorn Helgaas | f301ae6 | 2006-03-27 01:17:04 -0800 | [diff] [blame] | 62 | static int pnp_registered; |
| 63 | static unsigned int snd_mpu401_devices; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 65 | static int snd_mpu401_create(int dev, struct snd_card **rcard) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 67 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | int err; |
| 69 | |
| 70 | *rcard = NULL; |
| 71 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0); |
| 72 | if (card == NULL) |
| 73 | return -ENOMEM; |
| 74 | strcpy(card->driver, "MPU-401 UART"); |
| 75 | strcpy(card->shortname, card->driver); |
| 76 | sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]); |
| 77 | if (irq[dev] >= 0) { |
| 78 | sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]); |
| 79 | } else { |
| 80 | strcat(card->longname, "polled"); |
| 81 | } |
| 82 | |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 83 | if ((err = snd_mpu401_uart_new(card, 0, |
| 84 | MPU401_HW_MPU401, |
| 85 | port[dev], 0, |
| 86 | irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 88 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | *rcard = card; |
| 92 | return 0; |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 93 | |
| 94 | _err: |
| 95 | snd_card_free(card); |
| 96 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 99 | static int __devinit snd_mpu401_probe(struct platform_device *devptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 101 | int dev = devptr->id; |
| 102 | int err; |
| 103 | struct snd_card *card; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (port[dev] == SNDRV_AUTO_PORT) { |
| 106 | snd_printk(KERN_ERR "specify port\n"); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | if (irq[dev] == SNDRV_AUTO_IRQ) { |
| 110 | snd_printk(KERN_ERR "specify or disable IRQ\n"); |
| 111 | return -EINVAL; |
| 112 | } |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 113 | err = snd_mpu401_create(dev, &card); |
| 114 | if (err < 0) |
| 115 | return err; |
| 116 | snd_card_set_dev(card, &devptr->dev); |
| 117 | if ((err = snd_card_register(card)) < 0) { |
| 118 | snd_card_free(card); |
| 119 | return err; |
| 120 | } |
| 121 | platform_set_drvdata(devptr, card); |
| 122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 125 | static int __devexit snd_mpu401_remove(struct platform_device *devptr) |
| 126 | { |
| 127 | snd_card_free(platform_get_drvdata(devptr)); |
| 128 | platform_set_drvdata(devptr, NULL); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | #define SND_MPU401_DRIVER "snd_mpu401" |
| 133 | |
| 134 | static struct platform_driver snd_mpu401_driver = { |
| 135 | .probe = snd_mpu401_probe, |
| 136 | .remove = __devexit_p(snd_mpu401_remove), |
| 137 | .driver = { |
| 138 | .name = SND_MPU401_DRIVER |
| 139 | }, |
| 140 | }; |
| 141 | |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | #ifdef CONFIG_PNP |
| 144 | |
| 145 | #define IO_EXTENT 2 |
| 146 | |
| 147 | static struct pnp_device_id snd_mpu401_pnpids[] = { |
| 148 | { .id = "PNPb006" }, |
| 149 | { .id = "" } |
| 150 | }; |
| 151 | |
| 152 | MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids); |
| 153 | |
Andrew Morton | fad4348 | 2006-05-20 15:00:34 -0700 | [diff] [blame] | 154 | static int __devinit snd_mpu401_pnp(int dev, struct pnp_dev *device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | const struct pnp_device_id *id) |
| 156 | { |
| 157 | if (!pnp_port_valid(device, 0) || |
| 158 | pnp_port_flags(device, 0) & IORESOURCE_DISABLED) { |
| 159 | snd_printk(KERN_ERR "no PnP port\n"); |
| 160 | return -ENODEV; |
| 161 | } |
| 162 | if (pnp_port_len(device, 0) < IO_EXTENT) { |
Greg Kroah-Hartman | aa0a2dd | 2006-06-12 14:50:27 -0700 | [diff] [blame] | 163 | snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n", |
| 164 | (unsigned long long)pnp_port_len(device, 0), |
| 165 | IO_EXTENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return -ENODEV; |
| 167 | } |
| 168 | port[dev] = pnp_port_start(device, 0); |
| 169 | |
| 170 | if (!pnp_irq_valid(device, 0) || |
| 171 | pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) { |
| 172 | snd_printk(KERN_WARNING "no PnP irq, using polling\n"); |
| 173 | irq[dev] = -1; |
| 174 | } else { |
| 175 | irq[dev] = pnp_irq(device, 0); |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev, |
| 181 | const struct pnp_device_id *id) |
| 182 | { |
| 183 | static int dev; |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 184 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int err; |
| 186 | |
| 187 | for ( ; dev < SNDRV_CARDS; ++dev) { |
| 188 | if (!enable[dev] || !pnp[dev]) |
| 189 | continue; |
| 190 | err = snd_mpu401_pnp(dev, pnp_dev, id); |
| 191 | if (err < 0) |
| 192 | return err; |
| 193 | err = snd_mpu401_create(dev, &card); |
| 194 | if (err < 0) |
| 195 | return err; |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 196 | if ((err = snd_card_register(card)) < 0) { |
| 197 | snd_card_free(card); |
| 198 | return err; |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | snd_card_set_dev(card, &pnp_dev->dev); |
| 201 | pnp_set_drvdata(pnp_dev, card); |
Bjorn Helgaas | f301ae6 | 2006-03-27 01:17:04 -0800 | [diff] [blame] | 202 | snd_mpu401_devices++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | ++dev; |
| 204 | return 0; |
| 205 | } |
| 206 | return -ENODEV; |
| 207 | } |
| 208 | |
| 209 | static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev) |
| 210 | { |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 211 | struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | snd_card_disconnect(card); |
| 214 | snd_card_free_in_thread(card); |
| 215 | } |
| 216 | |
| 217 | static struct pnp_driver snd_mpu401_pnp_driver = { |
| 218 | .name = "mpu401", |
| 219 | .id_table = snd_mpu401_pnpids, |
| 220 | .probe = snd_mpu401_pnp_probe, |
| 221 | .remove = __devexit_p(snd_mpu401_pnp_remove), |
| 222 | }; |
| 223 | #else |
| 224 | static struct pnp_driver snd_mpu401_pnp_driver; |
| 225 | #endif |
| 226 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 227 | static void __init_or_module snd_mpu401_unregister_all(void) |
| 228 | { |
| 229 | int i; |
| 230 | |
| 231 | if (pnp_registered) |
| 232 | pnp_unregister_driver(&snd_mpu401_pnp_driver); |
| 233 | for (i = 0; i < ARRAY_SIZE(platform_devices); ++i) |
| 234 | platform_device_unregister(platform_devices[i]); |
| 235 | platform_driver_unregister(&snd_mpu401_driver); |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | static int __init alsa_card_mpu401_init(void) |
| 239 | { |
Bjorn Helgaas | f301ae6 | 2006-03-27 01:17:04 -0800 | [diff] [blame] | 240 | int i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 242 | if ((err = platform_driver_register(&snd_mpu401_driver)) < 0) |
| 243 | return err; |
| 244 | |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 245 | for (i = 0; i < SNDRV_CARDS; i++) { |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 246 | struct platform_device *device; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 247 | if (! enable[i]) |
| 248 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | #ifdef CONFIG_PNP |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 250 | if (pnp[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | continue; |
| 252 | #endif |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame] | 253 | device = platform_device_register_simple(SND_MPU401_DRIVER, |
| 254 | i, NULL, 0); |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 255 | if (IS_ERR(device)) |
| 256 | continue; |
Rene Herman | 7152447 | 2006-04-13 12:58:06 +0200 | [diff] [blame] | 257 | if (!platform_get_drvdata(device)) { |
| 258 | platform_device_unregister(device); |
| 259 | continue; |
| 260 | } |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 261 | platform_devices[i] = device; |
Bjorn Helgaas | f301ae6 | 2006-03-27 01:17:04 -0800 | [diff] [blame] | 262 | snd_mpu401_devices++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
Bjorn Helgaas | f301ae6 | 2006-03-27 01:17:04 -0800 | [diff] [blame] | 264 | err = pnp_register_driver(&snd_mpu401_pnp_driver); |
| 265 | if (!err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | pnp_registered = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Bjorn Helgaas | f301ae6 | 2006-03-27 01:17:04 -0800 | [diff] [blame] | 268 | if (!snd_mpu401_devices) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | #ifdef MODULE |
| 270 | printk(KERN_ERR "MPU-401 device not found or device busy\n"); |
| 271 | #endif |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 272 | snd_mpu401_unregister_all(); |
| 273 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static void __exit alsa_card_mpu401_exit(void) |
| 279 | { |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 280 | snd_mpu401_unregister_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | module_init(alsa_card_mpu401_init) |
| 284 | module_exit(alsa_card_mpu401_exit) |