blob: da1a29bfc85d7a21c91f8efb6f29edaae6fa0677 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic MPU-401 boards (UART mode only)
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/pnp.h>
Takashi Iwaib3fe9512005-11-17 16:03:39 +010025#include <linux/err.h>
26#include <linux/platform_device.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/mpu401.h>
30#include <sound/initval.h>
31
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020032MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_DESCRIPTION("MPU-401 UART");
34MODULE_LICENSE("GPL");
35
36static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
37static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103038static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +103040static bool pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#endif
42static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
43static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
Rusty Russella67ff6a2011-12-15 13:49:36 +103044static bool uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46module_param_array(index, int, NULL, 0444);
47MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
48module_param_array(id, charp, NULL, 0444);
49MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
50module_param_array(enable, bool, NULL, 0444);
51MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
52#ifdef CONFIG_PNP
53module_param_array(pnp, bool, NULL, 0444);
54MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
55#endif
56module_param_array(port, long, NULL, 0444);
57MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
58module_param_array(irq, int, NULL, 0444);
59MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
Takashi Iwai8f7ba052007-02-22 16:07:21 +010060module_param_array(uart_enter, bool, NULL, 0444);
61MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Clemens Ladischf7a92752005-12-07 09:13:42 +010063static struct platform_device *platform_devices[SNDRV_CARDS];
Bjorn Helgaasf301ae62006-03-27 01:17:04 -080064static int pnp_registered;
65static unsigned int snd_mpu401_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Takashi Iwaie1fad172005-11-17 14:12:45 +010067static int snd_mpu401_create(int dev, struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Takashi Iwaie1fad172005-11-17 14:12:45 +010069 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 int err;
71
Clemens Ladischc1099fc2007-10-11 14:42:23 +020072 if (!uart_enter[dev])
73 snd_printk(KERN_ERR "the uart_enter option is obsolete; remove it\n");
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *rcard = NULL;
Takashi Iwaibd7dd772008-12-28 16:45:02 +010076 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
77 if (err < 0)
78 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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 Ladischc1099fc2007-10-11 14:42:23 +020088 err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
Clemens Ladischdba8b462011-09-13 11:24:41 +020089 irq[dev], NULL);
Takashi Iwai8f7ba052007-02-22 16:07:21 +010090 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
Takashi Iwai16dab542005-09-05 17:17:58 +020092 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Takashi Iwai16dab542005-09-05 17:17:58 +020094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *rcard = card;
96 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +020097
98 _err:
99 snd_card_free(card);
100 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500103static int snd_mpu401_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100105 int dev = devptr->id;
106 int err;
107 struct snd_card *card;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (port[dev] == SNDRV_AUTO_PORT) {
110 snd_printk(KERN_ERR "specify port\n");
111 return -EINVAL;
112 }
113 if (irq[dev] == SNDRV_AUTO_IRQ) {
114 snd_printk(KERN_ERR "specify or disable IRQ\n");
115 return -EINVAL;
116 }
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100117 err = snd_mpu401_create(dev, &card);
118 if (err < 0)
119 return err;
120 snd_card_set_dev(card, &devptr->dev);
121 if ((err = snd_card_register(card)) < 0) {
122 snd_card_free(card);
123 return err;
124 }
125 platform_set_drvdata(devptr, card);
126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500129static int snd_mpu401_remove(struct platform_device *devptr)
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100130{
131 snd_card_free(platform_get_drvdata(devptr));
132 platform_set_drvdata(devptr, NULL);
133 return 0;
134}
135
136#define SND_MPU401_DRIVER "snd_mpu401"
137
138static struct platform_driver snd_mpu401_driver = {
139 .probe = snd_mpu401_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500140 .remove = snd_mpu401_remove,
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100141 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +0200142 .name = SND_MPU401_DRIVER,
143 .owner = THIS_MODULE,
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100144 },
145};
146
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#ifdef CONFIG_PNP
149
150#define IO_EXTENT 2
151
152static struct pnp_device_id snd_mpu401_pnpids[] = {
153 { .id = "PNPb006" },
154 { .id = "" }
155};
156
157MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
158
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500159static int snd_mpu401_pnp(int dev, struct pnp_dev *device,
160 const struct pnp_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
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-Hartmanaa0a2dd2006-06-12 14:50:27 -0700168 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 Torvalds1da177e2005-04-16 15:20:36 -0700171 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
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500185static int snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
186 const struct pnp_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 static int dev;
Takashi Iwaie1fad172005-11-17 14:12:45 +0100189 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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 Iwaib3fe9512005-11-17 16:03:39 +0100201 if ((err = snd_card_register(card)) < 0) {
202 snd_card_free(card);
203 return err;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 snd_card_set_dev(card, &pnp_dev->dev);
206 pnp_set_drvdata(pnp_dev, card);
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800207 snd_mpu401_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ++dev;
209 return 0;
210 }
211 return -ENODEV;
212}
213
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500214static void snd_mpu401_pnp_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Takashi Iwaie1fad172005-11-17 14:12:45 +0100216 struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 snd_card_disconnect(card);
Takashi Iwai2b29b132006-06-23 14:38:26 +0200219 snd_card_free_when_closed(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static struct pnp_driver snd_mpu401_pnp_driver = {
223 .name = "mpu401",
224 .id_table = snd_mpu401_pnpids,
225 .probe = snd_mpu401_pnp_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500226 .remove = snd_mpu401_pnp_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228#else
229static struct pnp_driver snd_mpu401_pnp_driver;
230#endif
231
Randy Dunlapc12aad62007-06-25 12:08:01 +0200232static void snd_mpu401_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100233{
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 Torvalds1da177e2005-04-16 15:20:36 -0700243static int __init alsa_card_mpu401_init(void)
244{
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800245 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100247 if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
248 return err;
249
Takashi Iwai8278ca82006-02-20 11:57:34 +0100250 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100251 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100252 if (! enable[i])
253 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#ifdef CONFIG_PNP
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100255 if (pnp[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 continue;
257#endif
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100258 device = platform_device_register_simple(SND_MPU401_DRIVER,
259 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200260 if (IS_ERR(device))
261 continue;
Rene Herman71524472006-04-13 12:58:06 +0200262 if (!platform_get_drvdata(device)) {
263 platform_device_unregister(device);
264 continue;
265 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100266 platform_devices[i] = device;
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800267 snd_mpu401_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800269 err = pnp_register_driver(&snd_mpu401_pnp_driver);
270 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 pnp_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800273 if (!snd_mpu401_devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#ifdef MODULE
275 printk(KERN_ERR "MPU-401 device not found or device busy\n");
276#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200277 snd_mpu401_unregister_all();
278 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 return 0;
281}
282
283static void __exit alsa_card_mpu401_exit(void)
284{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100285 snd_mpu401_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288module_init(alsa_card_mpu401_init)
289module_exit(alsa_card_mpu401_exit)