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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int pnp_registered = 0; |
| 62 | |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 63 | static int snd_mpu401_create(int dev, struct snd_card **rcard) |
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 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | int err; |
| 67 | |
| 68 | *rcard = NULL; |
| 69 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0); |
| 70 | if (card == NULL) |
| 71 | return -ENOMEM; |
| 72 | strcpy(card->driver, "MPU-401 UART"); |
| 73 | strcpy(card->shortname, card->driver); |
| 74 | sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]); |
| 75 | if (irq[dev] >= 0) { |
| 76 | sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]); |
| 77 | } else { |
| 78 | strcat(card->longname, "polled"); |
| 79 | } |
| 80 | |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 81 | if ((err = snd_mpu401_uart_new(card, 0, |
| 82 | MPU401_HW_MPU401, |
| 83 | port[dev], 0, |
| 84 | irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 86 | goto _err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | *rcard = card; |
| 90 | return 0; |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 91 | |
| 92 | _err: |
| 93 | snd_card_free(card); |
| 94 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 97 | static int __devinit snd_mpu401_probe(struct platform_device *devptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 99 | int dev = devptr->id; |
| 100 | int err; |
| 101 | struct snd_card *card; |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (port[dev] == SNDRV_AUTO_PORT) { |
| 104 | snd_printk(KERN_ERR "specify port\n"); |
| 105 | return -EINVAL; |
| 106 | } |
| 107 | if (irq[dev] == SNDRV_AUTO_IRQ) { |
| 108 | snd_printk(KERN_ERR "specify or disable IRQ\n"); |
| 109 | return -EINVAL; |
| 110 | } |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 111 | err = snd_mpu401_create(dev, &card); |
| 112 | if (err < 0) |
| 113 | return err; |
| 114 | snd_card_set_dev(card, &devptr->dev); |
| 115 | if ((err = snd_card_register(card)) < 0) { |
| 116 | snd_card_free(card); |
| 117 | return err; |
| 118 | } |
| 119 | platform_set_drvdata(devptr, card); |
| 120 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 123 | static int __devexit snd_mpu401_remove(struct platform_device *devptr) |
| 124 | { |
| 125 | snd_card_free(platform_get_drvdata(devptr)); |
| 126 | platform_set_drvdata(devptr, NULL); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | #define SND_MPU401_DRIVER "snd_mpu401" |
| 131 | |
| 132 | static struct platform_driver snd_mpu401_driver = { |
| 133 | .probe = snd_mpu401_probe, |
| 134 | .remove = __devexit_p(snd_mpu401_remove), |
| 135 | .driver = { |
| 136 | .name = SND_MPU401_DRIVER |
| 137 | }, |
| 138 | }; |
| 139 | |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | #ifdef CONFIG_PNP |
| 142 | |
| 143 | #define IO_EXTENT 2 |
| 144 | |
| 145 | static struct pnp_device_id snd_mpu401_pnpids[] = { |
| 146 | { .id = "PNPb006" }, |
| 147 | { .id = "" } |
| 148 | }; |
| 149 | |
| 150 | MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids); |
| 151 | |
| 152 | static int __init snd_mpu401_pnp(int dev, struct pnp_dev *device, |
| 153 | const struct pnp_device_id *id) |
| 154 | { |
| 155 | if (!pnp_port_valid(device, 0) || |
| 156 | pnp_port_flags(device, 0) & IORESOURCE_DISABLED) { |
| 157 | snd_printk(KERN_ERR "no PnP port\n"); |
| 158 | return -ENODEV; |
| 159 | } |
| 160 | if (pnp_port_len(device, 0) < IO_EXTENT) { |
| 161 | snd_printk(KERN_ERR "PnP port length is %ld, expected %d\n", |
| 162 | pnp_port_len(device, 0), IO_EXTENT); |
| 163 | return -ENODEV; |
| 164 | } |
| 165 | port[dev] = pnp_port_start(device, 0); |
| 166 | |
| 167 | if (!pnp_irq_valid(device, 0) || |
| 168 | pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) { |
| 169 | snd_printk(KERN_WARNING "no PnP irq, using polling\n"); |
| 170 | irq[dev] = -1; |
| 171 | } else { |
| 172 | irq[dev] = pnp_irq(device, 0); |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev, |
| 178 | const struct pnp_device_id *id) |
| 179 | { |
| 180 | static int dev; |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 181 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | int err; |
| 183 | |
| 184 | for ( ; dev < SNDRV_CARDS; ++dev) { |
| 185 | if (!enable[dev] || !pnp[dev]) |
| 186 | continue; |
| 187 | err = snd_mpu401_pnp(dev, pnp_dev, id); |
| 188 | if (err < 0) |
| 189 | return err; |
| 190 | err = snd_mpu401_create(dev, &card); |
| 191 | if (err < 0) |
| 192 | return err; |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 193 | if ((err = snd_card_register(card)) < 0) { |
| 194 | snd_card_free(card); |
| 195 | return err; |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | snd_card_set_dev(card, &pnp_dev->dev); |
| 198 | pnp_set_drvdata(pnp_dev, card); |
| 199 | ++dev; |
| 200 | return 0; |
| 201 | } |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev) |
| 206 | { |
Takashi Iwai | e1fad17 | 2005-11-17 14:12:45 +0100 | [diff] [blame] | 207 | struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | snd_card_disconnect(card); |
| 210 | snd_card_free_in_thread(card); |
| 211 | } |
| 212 | |
| 213 | static struct pnp_driver snd_mpu401_pnp_driver = { |
| 214 | .name = "mpu401", |
| 215 | .id_table = snd_mpu401_pnpids, |
| 216 | .probe = snd_mpu401_pnp_probe, |
| 217 | .remove = __devexit_p(snd_mpu401_pnp_remove), |
| 218 | }; |
| 219 | #else |
| 220 | static struct pnp_driver snd_mpu401_pnp_driver; |
| 221 | #endif |
| 222 | |
| 223 | static int __init alsa_card_mpu401_init(void) |
| 224 | { |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 225 | int i, err, devices; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 227 | if ((err = platform_driver_register(&snd_mpu401_driver)) < 0) |
| 228 | return err; |
| 229 | |
| 230 | devices = 0; |
| 231 | for (i = 0; i < SNDRV_CARDS && enable[i]; i++) { |
| 232 | struct platform_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | #ifdef CONFIG_PNP |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 234 | if (pnp[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | continue; |
| 236 | #endif |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 237 | device = platform_device_register_simple(SND_MPU401_DRIVER, |
| 238 | i, NULL, 0); |
| 239 | if (IS_ERR(device)) { |
| 240 | err = PTR_ERR(device); |
| 241 | goto errout; |
| 242 | } |
| 243 | devices++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | if ((err = pnp_register_driver(&snd_mpu401_pnp_driver)) >= 0) { |
| 246 | pnp_registered = 1; |
| 247 | devices += err; |
| 248 | } |
| 249 | |
| 250 | if (!devices) { |
| 251 | #ifdef MODULE |
| 252 | printk(KERN_ERR "MPU-401 device not found or device busy\n"); |
| 253 | #endif |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 254 | err = -ENODEV; |
| 255 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | return 0; |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 258 | |
| 259 | errout: |
| 260 | if (pnp_registered) |
| 261 | pnp_unregister_driver(&snd_mpu401_pnp_driver); |
| 262 | platform_driver_unregister(&snd_mpu401_driver); |
| 263 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void __exit alsa_card_mpu401_exit(void) |
| 267 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (pnp_registered) |
| 269 | pnp_unregister_driver(&snd_mpu401_pnp_driver); |
Takashi Iwai | b3fe951 | 2005-11-17 16:03:39 +0100 | [diff] [blame^] | 270 | platform_driver_unregister(&snd_mpu401_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | module_init(alsa_card_mpu401_init) |
| 274 | module_exit(alsa_card_mpu401_exit) |