patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / drivers / mpu401 / mpu401.c
1 /*
2  *  Driver for generic MPU-401 boards (UART mode only)
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *  ACPI PnP Copyright (c) 2004 by Clemens Ladisch <clemens@ladisch.de>
6  *  based on 8250_acpi.c
7  *  Copyright (c) 2002-2003 Matthew Wilcox for Hewlett-Packard
8  *  Copyright (C) 2004 Hewlett-Packard Co
9  *       Bjorn Helgaas <bjorn.helgaas@hp.com>
10  *
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  *
26  */
27
28 #include <sound/driver.h>
29 #include <linux/init.h>
30 #ifdef CONFIG_ACPI_BUS
31 #include <acpi/acpi_bus.h>
32 #endif
33 #include <linux/moduleparam.h>
34 #include <sound/core.h>
35 #include <sound/mpu401.h>
36 #include <sound/initval.h>
37
38 #ifdef CONFIG_ACPI_BUS
39 #define USE_ACPI_PNP
40 #endif
41
42 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
43 MODULE_DESCRIPTION("MPU-401 UART");
44 MODULE_LICENSE("GPL");
45 MODULE_CLASSES("{sound}");
46
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
50 #ifdef USE_ACPI_PNP
51 static int acpipnp[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
52 #endif
53 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* MPU-401 port number */
54 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* MPU-401 IRQ */
55 #ifdef CONFIG_X86_PC9800
56 static int pc98ii[SNDRV_CARDS];                         /* PC98-II dauther board */
57 #endif
58 static int boot_devs;
59
60 module_param_array(index, int, boot_devs, 0444);
61 MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
62 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
63 module_param_array(id, charp, boot_devs, 0444);
64 MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
65 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
66 module_param_array(enable, bool, boot_devs, 0444);
67 MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
68 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
69 #ifdef USE_ACPI_PNP
70 module_param_array(acpipnp, bool, boot_devs, 0444);
71 MODULE_PARM_DESC(acpipnp, "ACPI PnP detection for MPU-401 device.");
72 MODULE_PARM_SYNTAX(acpipnp, SNDRV_ENABLED "," SNDRV_BOOLEAN_TRUE_DESC);
73 #endif
74 module_param_array(port, long, boot_devs, 0444);
75 MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
76 MODULE_PARM_SYNTAX(port, SNDRV_PORT12_DESC);
77 module_param_array(irq, int, boot_devs, 0444);
78 MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
79 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
80 #ifdef CONFIG_X86_PC9800
81 module_param_array(pc98ii, bool, boot_devs, 0444);
82 MODULE_PARM_DESC(pc98ii, "Roland MPU-PC98II support.");
83 MODULE_PARM_SYNTAX(pc98ii, SNDRV_BOOLEAN_FALSE_DESC);
84 #endif
85
86 #ifndef CONFIG_ACPI_BUS
87 struct acpi_device;
88 #endif
89
90 static snd_card_t *snd_mpu401_legacy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
91 static int cards;
92
93 #ifdef USE_ACPI_PNP
94
95 static int acpi_driver_registered;
96
97 struct mpu401_resources {
98         unsigned long port;
99         int irq;
100 };
101
102 static acpi_status __devinit snd_mpu401_acpi_resource(struct acpi_resource *res,
103                                                       void *data)
104 {
105         struct mpu401_resources *resources = (struct mpu401_resources *)data;
106
107         if (res->id == ACPI_RSTYPE_IRQ) {
108                 if (res->data.irq.number_of_interrupts > 0) {
109 #ifdef CONFIG_IA64
110                         resources->irq = acpi_register_irq(res->data.irq.interrupts[0],
111                                                            res->data.irq.active_high_low,
112                                                            res->data.irq.edge_level);
113 #else
114                         resources->irq = res->data.irq.interrupts[0];
115 #endif
116                 }
117         } else if (res->id == ACPI_RSTYPE_IO) {
118                 if (res->data.io.range_length >= 2) {
119                         resources->port = res->data.io.min_base_address;
120                 }
121         }
122         return AE_OK;
123 }
124
125 static int __devinit snd_mpu401_acpi_pnp(int dev, struct acpi_device *device)
126 {
127         struct mpu401_resources res;
128         acpi_status status;
129
130         res.port = SNDRV_AUTO_PORT;
131         res.irq = SNDRV_AUTO_IRQ;
132         status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
133                                      snd_mpu401_acpi_resource, &res);
134         if (ACPI_FAILURE(status))
135                 return -ENODEV;
136         if (res.port == SNDRV_AUTO_PORT || res.irq == SNDRV_AUTO_IRQ) {
137                 snd_printk(KERN_ERR "no port or irq in %s _CRS\n",
138                            acpi_device_bid(device));
139                 return -ENODEV;
140         }
141         port[dev] = res.port;
142         irq[dev] = res.irq;
143         return 0;
144 }
145
146 #endif /* USE_ACPI_PNP */
147
148 static int __devinit snd_card_mpu401_probe(int dev, struct acpi_device *device)
149 {
150         snd_card_t *card;
151         int err;
152
153 #ifdef USE_ACPI_PNP
154         if (!device) {
155 #endif
156                 if (port[dev] == SNDRV_AUTO_PORT) {
157                         snd_printk(KERN_ERR "specify port\n");
158                         return -EINVAL;
159                 }
160                 if (irq[dev] == SNDRV_AUTO_IRQ) {
161                         snd_printk(KERN_ERR "specify or disable IRQ port\n");
162                         return -EINVAL;
163                 }
164 #ifdef USE_ACPI_PNP
165         }
166 #endif
167
168 #ifdef USE_ACPI_PNP
169         if (device && (err = snd_mpu401_acpi_pnp(dev, device)) < 0)
170                 return err;
171 #endif
172
173         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
174         if (card == NULL)
175                 return -ENOMEM;
176         strcpy(card->driver, "MPU-401 UART");
177         strcpy(card->shortname, card->driver);
178         sprintf(card->longname, "%s at 0x%lx, ", card->shortname, port[dev]);
179         if (irq[dev] >= 0) {
180                 sprintf(card->longname + strlen(card->longname), "IRQ %d", irq[dev]);
181         } else {
182                 strcat(card->longname, "polled");
183         }
184 #ifdef USE_ACPI_PNP
185         if (device) {
186                 strcat(card->longname, ", bus id ");
187                 strlcat(card->longname, acpi_device_bid(device), sizeof(card->longname));
188         }
189 #endif
190         if (snd_mpu401_uart_new(card, 0,
191 #ifdef CONFIG_X86_PC9800
192                                 pc98ii[dev] ? MPU401_HW_PC98II :
193 #endif
194                                 MPU401_HW_MPU401,
195                                 port[dev], 0,
196                                 irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL) < 0) {
197                 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
198                 snd_card_free(card);
199                 return -ENODEV;
200         }
201         if ((err = snd_card_register(card)) < 0) {
202                 snd_card_free(card);
203                 return err;
204         }
205 #ifdef USE_ACPI_PNP
206         if (device)
207                 acpi_driver_data(device) = card;
208         else
209 #endif
210                 snd_mpu401_legacy_cards[dev] = card;
211         ++cards;
212         return 0;
213 }
214
215 #ifdef USE_ACPI_PNP
216
217 static int __devinit snd_mpu401_acpi_add(struct acpi_device *device)
218 {
219         static int dev;
220         int err;
221
222         for ( ; dev < SNDRV_CARDS; ++dev) {
223                 if (!enable[dev] || !acpipnp[dev])
224                         continue;
225                 err = snd_card_mpu401_probe(dev, device);
226                 if (err < 0)
227                         return err;
228                 ++dev;
229                 return 0;
230         }
231         return -ENODEV;
232 }
233
234 static int __devexit snd_mpu401_acpi_remove(struct acpi_device *device,
235                                             int type)
236 {
237         snd_card_t *card;
238
239         if (!device)
240                 return -EINVAL;
241         card = (snd_card_t *)acpi_driver_data(device);
242         if (!card)
243                 return -EINVAL;
244
245         snd_card_disconnect(card);
246         snd_card_free_in_thread(card);
247         acpi_driver_data(device) = NULL;
248         return 0;
249 }
250
251 static struct acpi_driver snd_mpu401_acpi_driver = {
252         .name = "MPU-401 Driver",
253         .class = "mpu401",
254         .ids = "PNPB006",
255         .ops = {
256                 .add = snd_mpu401_acpi_add,
257                 .remove = __devexit_p(snd_mpu401_acpi_remove),
258         },
259 };
260
261 #endif /* USE_ACPI_PNP */
262
263 static int __init alsa_card_mpu401_init(void)
264 {
265         int dev;
266
267 #ifdef USE_ACPI_PNP
268         if (acpi_bus_register_driver(&snd_mpu401_acpi_driver) >= 0)
269                 acpi_driver_registered = 1;
270 #endif
271         for (dev = 0; dev < SNDRV_CARDS; dev++) {
272                 if (!enable[dev])
273                         continue;
274 #ifdef USE_ACPI_PNP
275                 if (acpipnp[dev] && acpi_driver_registered)
276                         continue;
277 #endif
278                 snd_card_mpu401_probe(dev, NULL);
279         }
280         if (!cards) {
281 #ifdef MODULE
282                 printk(KERN_ERR "MPU-401 device not found or device busy\n");
283 #endif
284 #ifdef USE_ACPI_PNP
285                 if (acpi_driver_registered)
286                         acpi_bus_unregister_driver(&snd_mpu401_acpi_driver);
287 #endif
288                 return -ENODEV;
289         }
290         return 0;
291 }
292
293 static void __exit alsa_card_mpu401_exit(void)
294 {
295         int idx;
296
297 #ifdef USE_ACPI_PNP
298         if (acpi_driver_registered)
299                 acpi_bus_unregister_driver(&snd_mpu401_acpi_driver);
300 #endif
301         for (idx = 0; idx < SNDRV_CARDS; idx++)
302                 snd_card_free(snd_mpu401_legacy_cards[idx]);
303 }
304
305 module_init(alsa_card_mpu401_init)
306 module_exit(alsa_card_mpu401_exit)