blob: 23fee622c8fc50bfb0e659dc2991a27e43bf7e30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Iwaib3fe9512005-11-17 16:03:39 +010026#include <linux/err.h>
27#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/moduleparam.h>
29#include <sound/core.h>
30#include <sound/mpu401.h>
31#include <sound/initval.h>
32
33MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34MODULE_DESCRIPTION("MPU-401 UART");
35MODULE_LICENSE("GPL");
36
37static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
38static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
39static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
40#ifdef CONFIG_PNP
41static int pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
42#endif
43static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
44static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
Takashi Iwai8f7ba052007-02-22 16:07:21 +010045static int uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47module_param_array(index, int, NULL, 0444);
48MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
49module_param_array(id, charp, NULL, 0444);
50MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
51module_param_array(enable, bool, NULL, 0444);
52MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
53#ifdef CONFIG_PNP
54module_param_array(pnp, bool, NULL, 0444);
55MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
56#endif
57module_param_array(port, long, NULL, 0444);
58MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
59module_param_array(irq, int, NULL, 0444);
60MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
Takashi Iwai8f7ba052007-02-22 16:07:21 +010061module_param_array(uart_enter, bool, NULL, 0444);
62MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Clemens Ladischf7a92752005-12-07 09:13:42 +010064static struct platform_device *platform_devices[SNDRV_CARDS];
Bjorn Helgaasf301ae62006-03-27 01:17:04 -080065static int pnp_registered;
66static unsigned int snd_mpu401_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Takashi Iwaie1fad172005-11-17 14:12:45 +010068static int snd_mpu401_create(int dev, struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Takashi Iwaie1fad172005-11-17 14:12:45 +010070 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int err;
72
Clemens Ladischc1099fc2007-10-11 14:42:23 +020073 if (!uart_enter[dev])
74 snd_printk(KERN_ERR "the uart_enter option is obsolete; remove it\n");
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *rcard = NULL;
77 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
78 if (card == NULL)
79 return -ENOMEM;
80 strcpy(card->driver, "MPU-401 UART");
81 strcpy(card->shortname, card->driver);
82 sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
83 if (irq[dev] >= 0) {
84 sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
85 } else {
86 strcat(card->longname, "polled");
87 }
88
Clemens Ladischc1099fc2007-10-11 14:42:23 +020089 err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
Takashi Iwai8f7ba052007-02-22 16:07:21 +010090 irq[dev], irq[dev] >= 0 ? IRQF_DISABLED : 0,
91 NULL);
92 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
Takashi Iwai16dab542005-09-05 17:17:58 +020094 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Takashi Iwai16dab542005-09-05 17:17:58 +020096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *rcard = card;
98 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +020099
100 _err:
101 snd_card_free(card);
102 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100105static int __devinit snd_mpu401_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100107 int dev = devptr->id;
108 int err;
109 struct snd_card *card;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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 Iwaib3fe9512005-11-17 16:03:39 +0100119 err = snd_mpu401_create(dev, &card);
120 if (err < 0)
121 return err;
122 snd_card_set_dev(card, &devptr->dev);
123 if ((err = snd_card_register(card)) < 0) {
124 snd_card_free(card);
125 return err;
126 }
127 platform_set_drvdata(devptr, card);
128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100131static int __devexit snd_mpu401_remove(struct platform_device *devptr)
132{
133 snd_card_free(platform_get_drvdata(devptr));
134 platform_set_drvdata(devptr, NULL);
135 return 0;
136}
137
138#define SND_MPU401_DRIVER "snd_mpu401"
139
140static struct platform_driver snd_mpu401_driver = {
141 .probe = snd_mpu401_probe,
142 .remove = __devexit_p(snd_mpu401_remove),
143 .driver = {
144 .name = SND_MPU401_DRIVER
145 },
146};
147
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#ifdef CONFIG_PNP
150
151#define IO_EXTENT 2
152
153static struct pnp_device_id snd_mpu401_pnpids[] = {
154 { .id = "PNPb006" },
155 { .id = "" }
156};
157
158MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
159
Andrew Mortonfad43482006-05-20 15:00:34 -0700160static int __devinit snd_mpu401_pnp(int dev, struct pnp_dev *device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 const struct pnp_device_id *id)
162{
163 if (!pnp_port_valid(device, 0) ||
164 pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
165 snd_printk(KERN_ERR "no PnP port\n");
166 return -ENODEV;
167 }
168 if (pnp_port_len(device, 0) < IO_EXTENT) {
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -0700169 snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n",
170 (unsigned long long)pnp_port_len(device, 0),
171 IO_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -ENODEV;
173 }
174 port[dev] = pnp_port_start(device, 0);
175
176 if (!pnp_irq_valid(device, 0) ||
177 pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
178 snd_printk(KERN_WARNING "no PnP irq, using polling\n");
179 irq[dev] = -1;
180 } else {
181 irq[dev] = pnp_irq(device, 0);
182 }
183 return 0;
184}
185
186static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
187 const struct pnp_device_id *id)
188{
189 static int dev;
Takashi Iwaie1fad172005-11-17 14:12:45 +0100190 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int err;
192
193 for ( ; dev < SNDRV_CARDS; ++dev) {
194 if (!enable[dev] || !pnp[dev])
195 continue;
196 err = snd_mpu401_pnp(dev, pnp_dev, id);
197 if (err < 0)
198 return err;
199 err = snd_mpu401_create(dev, &card);
200 if (err < 0)
201 return err;
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100202 if ((err = snd_card_register(card)) < 0) {
203 snd_card_free(card);
204 return err;
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 snd_card_set_dev(card, &pnp_dev->dev);
207 pnp_set_drvdata(pnp_dev, card);
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800208 snd_mpu401_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ++dev;
210 return 0;
211 }
212 return -ENODEV;
213}
214
215static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
216{
Takashi Iwaie1fad172005-11-17 14:12:45 +0100217 struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 snd_card_disconnect(card);
Takashi Iwai2b29b132006-06-23 14:38:26 +0200220 snd_card_free_when_closed(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static struct pnp_driver snd_mpu401_pnp_driver = {
224 .name = "mpu401",
225 .id_table = snd_mpu401_pnpids,
226 .probe = snd_mpu401_pnp_probe,
227 .remove = __devexit_p(snd_mpu401_pnp_remove),
228};
229#else
230static struct pnp_driver snd_mpu401_pnp_driver;
231#endif
232
Randy Dunlapc12aad62007-06-25 12:08:01 +0200233static void snd_mpu401_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100234{
235 int i;
236
237 if (pnp_registered)
238 pnp_unregister_driver(&snd_mpu401_pnp_driver);
239 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
240 platform_device_unregister(platform_devices[i]);
241 platform_driver_unregister(&snd_mpu401_driver);
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int __init alsa_card_mpu401_init(void)
245{
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800246 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100248 if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
249 return err;
250
Takashi Iwai8278ca82006-02-20 11:57:34 +0100251 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100252 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100253 if (! enable[i])
254 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#ifdef CONFIG_PNP
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100256 if (pnp[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 continue;
258#endif
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100259 device = platform_device_register_simple(SND_MPU401_DRIVER,
260 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200261 if (IS_ERR(device))
262 continue;
Rene Herman71524472006-04-13 12:58:06 +0200263 if (!platform_get_drvdata(device)) {
264 platform_device_unregister(device);
265 continue;
266 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100267 platform_devices[i] = device;
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800268 snd_mpu401_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800270 err = pnp_register_driver(&snd_mpu401_pnp_driver);
271 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pnp_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800274 if (!snd_mpu401_devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#ifdef MODULE
276 printk(KERN_ERR "MPU-401 device not found or device busy\n");
277#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200278 snd_mpu401_unregister_all();
279 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 return 0;
282}
283
284static void __exit alsa_card_mpu401_exit(void)
285{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100286 snd_mpu401_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289module_init(alsa_card_mpu401_init)
290module_exit(alsa_card_mpu401_exit)