blob: 9d10d79e27afca0f9e3574497efe1816e137f269 [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 */
45
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.");
60
Clemens Ladischf7a92752005-12-07 09:13:42 +010061static struct platform_device *platform_devices[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int pnp_registered = 0;
63
Takashi Iwaie1fad172005-11-17 14:12:45 +010064static int snd_mpu401_create(int dev, struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Takashi Iwaie1fad172005-11-17 14:12:45 +010066 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int err;
68
69 *rcard = NULL;
70 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
71 if (card == NULL)
72 return -ENOMEM;
73 strcpy(card->driver, "MPU-401 UART");
74 strcpy(card->shortname, card->driver);
75 sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
76 if (irq[dev] >= 0) {
77 sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
78 } else {
79 strcat(card->longname, "polled");
80 }
81
Takashi Iwai16dab542005-09-05 17:17:58 +020082 if ((err = snd_mpu401_uart_new(card, 0,
83 MPU401_HW_MPU401,
84 port[dev], 0,
85 irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
Takashi Iwai16dab542005-09-05 17:17:58 +020087 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Takashi Iwai16dab542005-09-05 17:17:58 +020089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *rcard = card;
91 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +020092
93 _err:
94 snd_card_free(card);
95 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Takashi Iwaib3fe9512005-11-17 16:03:39 +010098static int __devinit snd_mpu401_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100100 int dev = devptr->id;
101 int err;
102 struct snd_card *card;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (port[dev] == SNDRV_AUTO_PORT) {
105 snd_printk(KERN_ERR "specify port\n");
106 return -EINVAL;
107 }
108 if (irq[dev] == SNDRV_AUTO_IRQ) {
109 snd_printk(KERN_ERR "specify or disable IRQ\n");
110 return -EINVAL;
111 }
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100112 err = snd_mpu401_create(dev, &card);
113 if (err < 0)
114 return err;
115 snd_card_set_dev(card, &devptr->dev);
116 if ((err = snd_card_register(card)) < 0) {
117 snd_card_free(card);
118 return err;
119 }
120 platform_set_drvdata(devptr, card);
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100124static int __devexit snd_mpu401_remove(struct platform_device *devptr)
125{
126 snd_card_free(platform_get_drvdata(devptr));
127 platform_set_drvdata(devptr, NULL);
128 return 0;
129}
130
131#define SND_MPU401_DRIVER "snd_mpu401"
132
133static struct platform_driver snd_mpu401_driver = {
134 .probe = snd_mpu401_probe,
135 .remove = __devexit_p(snd_mpu401_remove),
136 .driver = {
137 .name = SND_MPU401_DRIVER
138 },
139};
140
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#ifdef CONFIG_PNP
143
144#define IO_EXTENT 2
145
146static struct pnp_device_id snd_mpu401_pnpids[] = {
147 { .id = "PNPb006" },
148 { .id = "" }
149};
150
151MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
152
153static int __init snd_mpu401_pnp(int dev, struct pnp_dev *device,
154 const struct pnp_device_id *id)
155{
156 if (!pnp_port_valid(device, 0) ||
157 pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
158 snd_printk(KERN_ERR "no PnP port\n");
159 return -ENODEV;
160 }
161 if (pnp_port_len(device, 0) < IO_EXTENT) {
162 snd_printk(KERN_ERR "PnP port length is %ld, expected %d\n",
163 pnp_port_len(device, 0), IO_EXTENT);
164 return -ENODEV;
165 }
166 port[dev] = pnp_port_start(device, 0);
167
168 if (!pnp_irq_valid(device, 0) ||
169 pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
170 snd_printk(KERN_WARNING "no PnP irq, using polling\n");
171 irq[dev] = -1;
172 } else {
173 irq[dev] = pnp_irq(device, 0);
174 }
175 return 0;
176}
177
178static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
179 const struct pnp_device_id *id)
180{
181 static int dev;
Takashi Iwaie1fad172005-11-17 14:12:45 +0100182 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int err;
184
185 for ( ; dev < SNDRV_CARDS; ++dev) {
186 if (!enable[dev] || !pnp[dev])
187 continue;
188 err = snd_mpu401_pnp(dev, pnp_dev, id);
189 if (err < 0)
190 return err;
191 err = snd_mpu401_create(dev, &card);
192 if (err < 0)
193 return err;
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100194 if ((err = snd_card_register(card)) < 0) {
195 snd_card_free(card);
196 return err;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 snd_card_set_dev(card, &pnp_dev->dev);
199 pnp_set_drvdata(pnp_dev, card);
200 ++dev;
201 return 0;
202 }
203 return -ENODEV;
204}
205
206static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
207{
Takashi Iwaie1fad172005-11-17 14:12:45 +0100208 struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 snd_card_disconnect(card);
211 snd_card_free_in_thread(card);
212}
213
214static struct pnp_driver snd_mpu401_pnp_driver = {
215 .name = "mpu401",
216 .id_table = snd_mpu401_pnpids,
217 .probe = snd_mpu401_pnp_probe,
218 .remove = __devexit_p(snd_mpu401_pnp_remove),
219};
220#else
221static struct pnp_driver snd_mpu401_pnp_driver;
222#endif
223
Clemens Ladischf7a92752005-12-07 09:13:42 +0100224static void __init_or_module snd_mpu401_unregister_all(void)
225{
226 int i;
227
228 if (pnp_registered)
229 pnp_unregister_driver(&snd_mpu401_pnp_driver);
230 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
231 platform_device_unregister(platform_devices[i]);
232 platform_driver_unregister(&snd_mpu401_driver);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int __init alsa_card_mpu401_init(void)
236{
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100237 int i, err, devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100239 if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
240 return err;
241
242 devices = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100243 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100244 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100245 if (! enable[i])
246 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#ifdef CONFIG_PNP
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100248 if (pnp[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 continue;
250#endif
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100251 device = platform_device_register_simple(SND_MPU401_DRIVER,
252 i, NULL, 0);
253 if (IS_ERR(device)) {
254 err = PTR_ERR(device);
255 goto errout;
256 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100257 platform_devices[i] = device;
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100258 devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 if ((err = pnp_register_driver(&snd_mpu401_pnp_driver)) >= 0) {
261 pnp_registered = 1;
262 devices += err;
263 }
264
265 if (!devices) {
266#ifdef MODULE
267 printk(KERN_ERR "MPU-401 device not found or device busy\n");
268#endif
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100269 err = -ENODEV;
270 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 return 0;
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100273
274 errout:
Clemens Ladischf7a92752005-12-07 09:13:42 +0100275 snd_mpu401_unregister_all();
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static void __exit alsa_card_mpu401_exit(void)
280{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100281 snd_mpu401_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284module_init(alsa_card_mpu401_init)
285module_exit(alsa_card_mpu401_exit)