blob: 9ea3059a706499cd0da280fa63c1ce60f7ae1923 [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];
Bjorn Helgaasf301ae62006-03-27 01:17:04 -080062static int pnp_registered;
63static unsigned int snd_mpu401_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Takashi Iwaie1fad172005-11-17 14:12:45 +010065static int snd_mpu401_create(int dev, struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Takashi Iwaie1fad172005-11-17 14:12:45 +010067 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int err;
69
70 *rcard = NULL;
71 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
72 if (card == NULL)
73 return -ENOMEM;
74 strcpy(card->driver, "MPU-401 UART");
75 strcpy(card->shortname, card->driver);
76 sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
77 if (irq[dev] >= 0) {
78 sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
79 } else {
80 strcat(card->longname, "polled");
81 }
82
Takashi Iwai16dab542005-09-05 17:17:58 +020083 if ((err = snd_mpu401_uart_new(card, 0,
84 MPU401_HW_MPU401,
85 port[dev], 0,
86 irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
Takashi Iwai16dab542005-09-05 17:17:58 +020088 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Takashi Iwai16dab542005-09-05 17:17:58 +020090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *rcard = card;
92 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +020093
94 _err:
95 snd_card_free(card);
96 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Takashi Iwaib3fe9512005-11-17 16:03:39 +010099static int __devinit snd_mpu401_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100101 int dev = devptr->id;
102 int err;
103 struct snd_card *card;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (port[dev] == SNDRV_AUTO_PORT) {
106 snd_printk(KERN_ERR "specify port\n");
107 return -EINVAL;
108 }
109 if (irq[dev] == SNDRV_AUTO_IRQ) {
110 snd_printk(KERN_ERR "specify or disable IRQ\n");
111 return -EINVAL;
112 }
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100113 err = snd_mpu401_create(dev, &card);
114 if (err < 0)
115 return err;
116 snd_card_set_dev(card, &devptr->dev);
117 if ((err = snd_card_register(card)) < 0) {
118 snd_card_free(card);
119 return err;
120 }
121 platform_set_drvdata(devptr, card);
122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100125static int __devexit snd_mpu401_remove(struct platform_device *devptr)
126{
127 snd_card_free(platform_get_drvdata(devptr));
128 platform_set_drvdata(devptr, NULL);
129 return 0;
130}
131
132#define SND_MPU401_DRIVER "snd_mpu401"
133
134static struct platform_driver snd_mpu401_driver = {
135 .probe = snd_mpu401_probe,
136 .remove = __devexit_p(snd_mpu401_remove),
137 .driver = {
138 .name = SND_MPU401_DRIVER
139 },
140};
141
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#ifdef CONFIG_PNP
144
145#define IO_EXTENT 2
146
147static struct pnp_device_id snd_mpu401_pnpids[] = {
148 { .id = "PNPb006" },
149 { .id = "" }
150};
151
152MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
153
154static int __init snd_mpu401_pnp(int dev, struct pnp_dev *device,
155 const struct pnp_device_id *id)
156{
157 if (!pnp_port_valid(device, 0) ||
158 pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
159 snd_printk(KERN_ERR "no PnP port\n");
160 return -ENODEV;
161 }
162 if (pnp_port_len(device, 0) < IO_EXTENT) {
163 snd_printk(KERN_ERR "PnP port length is %ld, expected %d\n",
164 pnp_port_len(device, 0), IO_EXTENT);
165 return -ENODEV;
166 }
167 port[dev] = pnp_port_start(device, 0);
168
169 if (!pnp_irq_valid(device, 0) ||
170 pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
171 snd_printk(KERN_WARNING "no PnP irq, using polling\n");
172 irq[dev] = -1;
173 } else {
174 irq[dev] = pnp_irq(device, 0);
175 }
176 return 0;
177}
178
179static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
180 const struct pnp_device_id *id)
181{
182 static int dev;
Takashi Iwaie1fad172005-11-17 14:12:45 +0100183 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int err;
185
186 for ( ; dev < SNDRV_CARDS; ++dev) {
187 if (!enable[dev] || !pnp[dev])
188 continue;
189 err = snd_mpu401_pnp(dev, pnp_dev, id);
190 if (err < 0)
191 return err;
192 err = snd_mpu401_create(dev, &card);
193 if (err < 0)
194 return err;
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100195 if ((err = snd_card_register(card)) < 0) {
196 snd_card_free(card);
197 return err;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 snd_card_set_dev(card, &pnp_dev->dev);
200 pnp_set_drvdata(pnp_dev, card);
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800201 snd_mpu401_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 ++dev;
203 return 0;
204 }
205 return -ENODEV;
206}
207
208static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
209{
Takashi Iwaie1fad172005-11-17 14:12:45 +0100210 struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 snd_card_disconnect(card);
213 snd_card_free_in_thread(card);
214}
215
216static struct pnp_driver snd_mpu401_pnp_driver = {
217 .name = "mpu401",
218 .id_table = snd_mpu401_pnpids,
219 .probe = snd_mpu401_pnp_probe,
220 .remove = __devexit_p(snd_mpu401_pnp_remove),
221};
222#else
223static struct pnp_driver snd_mpu401_pnp_driver;
224#endif
225
Clemens Ladischf7a92752005-12-07 09:13:42 +0100226static void __init_or_module snd_mpu401_unregister_all(void)
227{
228 int i;
229
230 if (pnp_registered)
231 pnp_unregister_driver(&snd_mpu401_pnp_driver);
232 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
233 platform_device_unregister(platform_devices[i]);
234 platform_driver_unregister(&snd_mpu401_driver);
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int __init alsa_card_mpu401_init(void)
238{
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800239 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100241 if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
242 return err;
243
Takashi Iwai8278ca82006-02-20 11:57:34 +0100244 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100245 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100246 if (! enable[i])
247 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#ifdef CONFIG_PNP
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100249 if (pnp[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 continue;
251#endif
Takashi Iwaib3fe9512005-11-17 16:03:39 +0100252 device = platform_device_register_simple(SND_MPU401_DRIVER,
253 i, NULL, 0);
254 if (IS_ERR(device)) {
255 err = PTR_ERR(device);
256 goto errout;
257 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100258 platform_devices[i] = device;
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800259 snd_mpu401_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800261 err = pnp_register_driver(&snd_mpu401_pnp_driver);
262 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 pnp_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Bjorn Helgaasf301ae62006-03-27 01:17:04 -0800265 if (!snd_mpu401_devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#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)