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