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