vserver 1.9.5.x5
[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 <linux/acpi.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
46 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
47 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
48 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
49 #ifdef USE_ACPI_PNP
50 static int acpipnp[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
51 #endif
52 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* MPU-401 port number */
53 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* MPU-401 IRQ */
54
55 module_param_array(index, int, NULL, 0444);
56 MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
57 module_param_array(id, charp, NULL, 0444);
58 MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
59 module_param_array(enable, bool, NULL, 0444);
60 MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
61 #ifdef USE_ACPI_PNP
62 module_param_array(acpipnp, bool, NULL, 0444);
63 MODULE_PARM_DESC(acpipnp, "ACPI PnP detection for MPU-401 device.");
64 #endif
65 module_param_array(port, long, NULL, 0444);
66 MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
67 module_param_array(irq, int, NULL, 0444);
68 MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
69
70 #ifndef CONFIG_ACPI_BUS
71 struct acpi_device;
72 #endif
73
74 static snd_card_t *snd_mpu401_legacy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
75 static int cards;
76
77 #ifdef USE_ACPI_PNP
78
79 static int acpi_driver_registered;
80
81 struct mpu401_resources {
82         unsigned long port;
83         int irq;
84 };
85
86 static acpi_status __devinit snd_mpu401_acpi_resource(struct acpi_resource *res,
87                                                       void *data)
88 {
89         struct mpu401_resources *resources = (struct mpu401_resources *)data;
90
91         if (res->id == ACPI_RSTYPE_IRQ) {
92                 if (res->data.irq.number_of_interrupts > 0) {
93                         resources->irq = acpi_register_gsi(res->data.irq.interrupts[0],
94                                                            res->data.irq.edge_level,
95                                                            res->data.irq.active_high_low);
96                 }
97         } else if (res->id == ACPI_RSTYPE_IO) {
98                 if (res->data.io.range_length >= 2) {
99                         resources->port = res->data.io.min_base_address;
100                 }
101         }
102         return AE_OK;
103 }
104
105 static int __devinit snd_mpu401_acpi_pnp(int dev, struct acpi_device *device)
106 {
107         struct mpu401_resources res;
108         acpi_status status;
109
110         res.port = SNDRV_AUTO_PORT;
111         res.irq = SNDRV_AUTO_IRQ;
112         status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
113                                      snd_mpu401_acpi_resource, &res);
114         if (ACPI_FAILURE(status))
115                 return -ENODEV;
116         if (res.port == SNDRV_AUTO_PORT || res.irq == SNDRV_AUTO_IRQ) {
117                 snd_printk(KERN_ERR "no port or irq in %s _CRS\n",
118                            acpi_device_bid(device));
119                 return -ENODEV;
120         }
121         port[dev] = res.port;
122         irq[dev] = res.irq;
123         return 0;
124 }
125
126 #endif /* USE_ACPI_PNP */
127
128 static int __devinit snd_card_mpu401_probe(int dev, struct acpi_device *device)
129 {
130         snd_card_t *card;
131         int err;
132
133 #ifdef USE_ACPI_PNP
134         if (!device) {
135 #endif
136                 if (port[dev] == SNDRV_AUTO_PORT) {
137                         snd_printk(KERN_ERR "specify port\n");
138                         return -EINVAL;
139                 }
140                 if (irq[dev] == SNDRV_AUTO_IRQ) {
141                         snd_printk(KERN_ERR "specify or disable IRQ port\n");
142                         return -EINVAL;
143                 }
144 #ifdef USE_ACPI_PNP
145         }
146 #endif
147
148 #ifdef USE_ACPI_PNP
149         if (device && (err = snd_mpu401_acpi_pnp(dev, device)) < 0)
150                 return err;
151 #endif
152
153         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
154         if (card == NULL)
155                 return -ENOMEM;
156         strcpy(card->driver, "MPU-401 UART");
157         strcpy(card->shortname, card->driver);
158         sprintf(card->longname, "%s at 0x%lx, ", card->shortname, port[dev]);
159         if (irq[dev] >= 0) {
160                 sprintf(card->longname + strlen(card->longname), "IRQ %d", irq[dev]);
161         } else {
162                 strcat(card->longname, "polled");
163         }
164 #ifdef USE_ACPI_PNP
165         if (device) {
166                 strcat(card->longname, ", ACPI id ");
167                 strlcat(card->longname, acpi_device_bid(device), sizeof(card->longname));
168         }
169 #endif
170         if (snd_mpu401_uart_new(card, 0,
171                                 MPU401_HW_MPU401,
172                                 port[dev], 0,
173                                 irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL) < 0) {
174                 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
175                 snd_card_free(card);
176                 return -ENODEV;
177         }
178         if ((err = snd_card_register(card)) < 0) {
179                 snd_card_free(card);
180                 return err;
181         }
182 #ifdef USE_ACPI_PNP
183         if (device)
184                 acpi_driver_data(device) = card;
185         else
186 #endif
187                 snd_mpu401_legacy_cards[dev] = card;
188         ++cards;
189         return 0;
190 }
191
192 #ifdef USE_ACPI_PNP
193
194 static int __devinit snd_mpu401_acpi_add(struct acpi_device *device)
195 {
196         static int dev;
197         int err;
198
199         for ( ; dev < SNDRV_CARDS; ++dev) {
200                 if (!enable[dev] || !acpipnp[dev])
201                         continue;
202                 err = snd_card_mpu401_probe(dev, device);
203                 if (err < 0)
204                         return err;
205                 ++dev;
206                 return 0;
207         }
208         return -ENODEV;
209 }
210
211 static int __devexit snd_mpu401_acpi_remove(struct acpi_device *device,
212                                             int type)
213 {
214         snd_card_t *card;
215
216         if (!device)
217                 return -EINVAL;
218         card = (snd_card_t *)acpi_driver_data(device);
219         if (!card)
220                 return -EINVAL;
221
222         snd_card_disconnect(card);
223         snd_card_free_in_thread(card);
224         acpi_driver_data(device) = NULL;
225         return 0;
226 }
227
228 static struct acpi_driver snd_mpu401_acpi_driver = {
229         .name = "MPU-401 Driver",
230         .class = "mpu401",
231         .ids = "PNPB006",
232         .ops = {
233                 .add = snd_mpu401_acpi_add,
234                 .remove = __devexit_p(snd_mpu401_acpi_remove),
235         },
236 };
237
238 #endif /* USE_ACPI_PNP */
239
240 static int __init alsa_card_mpu401_init(void)
241 {
242         int dev;
243
244 #ifdef USE_ACPI_PNP
245         if (acpi_bus_register_driver(&snd_mpu401_acpi_driver) >= 0)
246                 acpi_driver_registered = 1;
247 #endif
248         for (dev = 0; dev < SNDRV_CARDS; dev++) {
249                 if (!enable[dev])
250                         continue;
251 #ifdef USE_ACPI_PNP
252                 if (acpipnp[dev] && acpi_driver_registered)
253                         continue;
254 #endif
255                 snd_card_mpu401_probe(dev, NULL);
256         }
257         if (!cards) {
258 #ifdef MODULE
259                 printk(KERN_ERR "MPU-401 device not found or device busy\n");
260 #endif
261 #ifdef USE_ACPI_PNP
262                 if (acpi_driver_registered)
263                         acpi_bus_unregister_driver(&snd_mpu401_acpi_driver);
264 #endif
265                 return -ENODEV;
266         }
267         return 0;
268 }
269
270 static void __exit alsa_card_mpu401_exit(void)
271 {
272         int idx;
273
274 #ifdef USE_ACPI_PNP
275         if (acpi_driver_registered)
276                 acpi_bus_unregister_driver(&snd_mpu401_acpi_driver);
277 #endif
278         for (idx = 0; idx < SNDRV_CARDS; idx++)
279                 snd_card_free(snd_mpu401_legacy_cards[idx]);
280 }
281
282 module_init(alsa_card_mpu401_init)
283 module_exit(alsa_card_mpu401_exit)