]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/drivers/mpu401/mpu401.c
[ALSA] virmidi - Use platform_device
[net-next-2.6.git] / sound / drivers / mpu401 / mpu401.c
CommitLineData
1da177e4
LT
1/*
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>
26#include <linux/moduleparam.h>
27#include <sound/core.h>
28#include <sound/mpu401.h>
29#include <sound/initval.h>
30
31MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
32MODULE_DESCRIPTION("MPU-401 UART");
33MODULE_LICENSE("GPL");
34
35static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
36static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
37static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
38#ifdef CONFIG_PNP
39static int pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
40#endif
41static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
42static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
43
44module_param_array(index, int, NULL, 0444);
45MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
46module_param_array(id, charp, NULL, 0444);
47MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
48module_param_array(enable, bool, NULL, 0444);
49MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
50#ifdef CONFIG_PNP
51module_param_array(pnp, bool, NULL, 0444);
52MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
53#endif
54module_param_array(port, long, NULL, 0444);
55MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
56module_param_array(irq, int, NULL, 0444);
57MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
58
e1fad17b 59static struct snd_card *snd_mpu401_legacy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
1da177e4
LT
60static int pnp_registered = 0;
61
e1fad17b 62static int snd_mpu401_create(int dev, struct snd_card **rcard)
1da177e4 63{
e1fad17b 64 struct snd_card *card;
1da177e4
LT
65 int err;
66
67 *rcard = NULL;
68 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
69 if (card == NULL)
70 return -ENOMEM;
71 strcpy(card->driver, "MPU-401 UART");
72 strcpy(card->shortname, card->driver);
73 sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
74 if (irq[dev] >= 0) {
75 sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
76 } else {
77 strcat(card->longname, "polled");
78 }
79
16dab54b
TI
80 if ((err = snd_mpu401_uart_new(card, 0,
81 MPU401_HW_MPU401,
82 port[dev], 0,
83 irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL)) < 0) {
1da177e4 84 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
16dab54b 85 goto _err;
1da177e4 86 }
16dab54b
TI
87
88 if ((err = snd_card_set_generic_dev(card)) < 0)
89 goto _err;
90
91 if ((err = snd_card_register(card)) < 0)
92 goto _err;
93
1da177e4
LT
94 *rcard = card;
95 return 0;
16dab54b
TI
96
97 _err:
98 snd_card_free(card);
99 return err;
1da177e4
LT
100}
101
102static int __devinit snd_mpu401_probe(int dev)
103{
104 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 }
112 return snd_mpu401_create(dev, &snd_mpu401_legacy_cards[dev]);
113}
114
115#ifdef CONFIG_PNP
116
117#define IO_EXTENT 2
118
119static struct pnp_device_id snd_mpu401_pnpids[] = {
120 { .id = "PNPb006" },
121 { .id = "" }
122};
123
124MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
125
126static int __init snd_mpu401_pnp(int dev, struct pnp_dev *device,
127 const struct pnp_device_id *id)
128{
129 if (!pnp_port_valid(device, 0) ||
130 pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
131 snd_printk(KERN_ERR "no PnP port\n");
132 return -ENODEV;
133 }
134 if (pnp_port_len(device, 0) < IO_EXTENT) {
135 snd_printk(KERN_ERR "PnP port length is %ld, expected %d\n",
136 pnp_port_len(device, 0), IO_EXTENT);
137 return -ENODEV;
138 }
139 port[dev] = pnp_port_start(device, 0);
140
141 if (!pnp_irq_valid(device, 0) ||
142 pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
143 snd_printk(KERN_WARNING "no PnP irq, using polling\n");
144 irq[dev] = -1;
145 } else {
146 irq[dev] = pnp_irq(device, 0);
147 }
148 return 0;
149}
150
151static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
152 const struct pnp_device_id *id)
153{
154 static int dev;
e1fad17b 155 struct snd_card *card;
1da177e4
LT
156 int err;
157
158 for ( ; dev < SNDRV_CARDS; ++dev) {
159 if (!enable[dev] || !pnp[dev])
160 continue;
161 err = snd_mpu401_pnp(dev, pnp_dev, id);
162 if (err < 0)
163 return err;
164 err = snd_mpu401_create(dev, &card);
165 if (err < 0)
166 return err;
167 snd_card_set_dev(card, &pnp_dev->dev);
168 pnp_set_drvdata(pnp_dev, card);
169 ++dev;
170 return 0;
171 }
172 return -ENODEV;
173}
174
175static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
176{
e1fad17b 177 struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
1da177e4
LT
178
179 snd_card_disconnect(card);
180 snd_card_free_in_thread(card);
181}
182
183static struct pnp_driver snd_mpu401_pnp_driver = {
184 .name = "mpu401",
185 .id_table = snd_mpu401_pnpids,
186 .probe = snd_mpu401_pnp_probe,
187 .remove = __devexit_p(snd_mpu401_pnp_remove),
188};
189#else
190static struct pnp_driver snd_mpu401_pnp_driver;
191#endif
192
193static int __init alsa_card_mpu401_init(void)
194{
195 int dev, devices = 0;
196 int err;
197
198 for (dev = 0; dev < SNDRV_CARDS; dev++) {
199 if (!enable[dev])
200 continue;
201#ifdef CONFIG_PNP
202 if (pnp[dev])
203 continue;
204#endif
205 if (snd_mpu401_probe(dev) >= 0)
206 devices++;
207 }
208 if ((err = pnp_register_driver(&snd_mpu401_pnp_driver)) >= 0) {
209 pnp_registered = 1;
210 devices += err;
211 }
212
213 if (!devices) {
214#ifdef MODULE
215 printk(KERN_ERR "MPU-401 device not found or device busy\n");
216#endif
217 if (pnp_registered)
218 pnp_unregister_driver(&snd_mpu401_pnp_driver);
219 return -ENODEV;
220 }
221 return 0;
222}
223
224static void __exit alsa_card_mpu401_exit(void)
225{
226 int idx;
227
228 if (pnp_registered)
229 pnp_unregister_driver(&snd_mpu401_pnp_driver);
230 for (idx = 0; idx < SNDRV_CARDS; idx++)
231 snd_card_free(snd_mpu401_legacy_cards[idx]);
232}
233
234module_init(alsa_card_mpu401_init)
235module_exit(alsa_card_mpu401_exit)