ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <sound/core.h>
34 #include <sound/mpu401.h>
35 #define SNDRV_GET_ID
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
59 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
60 MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
61 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
62 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
63 MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
64 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
65 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
66 MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
67 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
68 #ifdef USE_ACPI_PNP
69 MODULE_PARM(acpipnp, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
70 MODULE_PARM_DESC(acpipnp, "ACPI PnP detection for MPU-401 device.");
71 MODULE_PARM_SYNTAX(acpipnp, SNDRV_ENABLED "," SNDRV_BOOLEAN_TRUE_DESC);
72 #endif
73 MODULE_PARM(port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
74 MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
75 MODULE_PARM_SYNTAX(port, SNDRV_PORT12_DESC);
76 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
77 MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
78 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
79 #ifdef CONFIG_X86_PC9800
80 MODULE_PARM(pc98ii, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
81 MODULE_PARM_DESC(pc98ii, "Roland MPU-PC98II support.");
82 MODULE_PARM_SYNTAX(pc98ii, SNDRV_BOOLEAN_FALSE_DESC);
83 #endif
84
85 #ifndef CONFIG_ACPI_BUS
86 struct acpi_device;
87 #endif
88
89 static snd_card_t *snd_mpu401_legacy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
90 static int cards;
91
92 #ifdef USE_ACPI_PNP
93
94 static int acpi_driver_registered;
95
96 struct mpu401_resources {
97         unsigned long port;
98         int irq;
99 };
100
101 static acpi_status __devinit snd_mpu401_acpi_resource(struct acpi_resource *res,
102                                                       void *data)
103 {
104         struct mpu401_resources *resources = (struct mpu401_resources *)data;
105
106         if (res->id == ACPI_RSTYPE_IRQ) {
107                 if (res->data.irq.number_of_interrupts > 0) {
108 #ifdef CONFIG_IA64
109                         resources->irq = acpi_register_irq(res->data.irq.interrupts[0],
110                                                            res->data.irq.active_high_low,
111                                                            res->data.irq.edge_level);
112 #else
113                         resources->irq = res->data.irq.interrupts[0];
114 #endif
115                 }
116         } else if (res->id == ACPI_RSTYPE_IO) {
117                 if (res->data.io.range_length >= 2) {
118                         resources->port = res->data.io.min_base_address;
119                 }
120         }
121         return AE_OK;
122 }
123
124 static int __devinit snd_mpu401_acpi_pnp(int dev, struct acpi_device *device)
125 {
126         struct mpu401_resources res;
127         acpi_status status;
128
129         res.port = SNDRV_AUTO_PORT;
130         res.irq = SNDRV_AUTO_IRQ;
131         status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
132                                      snd_mpu401_acpi_resource, &res);
133         if (ACPI_FAILURE(status))
134                 return -ENODEV;
135         if (res.port == SNDRV_AUTO_PORT || res.irq == SNDRV_AUTO_IRQ) {
136                 snd_printk(KERN_ERR "no port or irq in %s _CRS\n",
137                            acpi_device_bid(device));
138                 return -ENODEV;
139         }
140         port[dev] = res.port;
141         irq[dev] = res.irq;
142         return 0;
143 }
144
145 #endif /* USE_ACPI_PNP */
146
147 static int __devinit snd_card_mpu401_probe(int dev, struct acpi_device *device)
148 {
149         snd_card_t *card;
150         int err;
151
152 #ifdef USE_ACPI_PNP
153         if (!device) {
154 #endif
155                 if (port[dev] == SNDRV_AUTO_PORT) {
156                         snd_printk(KERN_ERR "specify port\n");
157                         return -EINVAL;
158                 }
159                 if (irq[dev] == SNDRV_AUTO_IRQ) {
160                         snd_printk(KERN_ERR "specify or disable IRQ port\n");
161                         return -EINVAL;
162                 }
163 #ifdef USE_ACPI_PNP
164         }
165 #endif
166
167 #ifdef USE_ACPI_PNP
168         if (device && (err = snd_mpu401_acpi_pnp(dev, device)) < 0)
169                 return err;
170 #endif
171
172         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
173         if (card == NULL)
174                 return -ENOMEM;
175         strcpy(card->driver, "MPU-401 UART");
176         strcpy(card->shortname, card->driver);
177         sprintf(card->longname, "%s at 0x%lx, ", card->shortname, port[dev]);
178         if (irq[dev] >= 0) {
179                 sprintf(card->longname + strlen(card->longname), "IRQ %d", irq[dev]);
180         } else {
181                 strcat(card->longname, "polled");
182         }
183 #ifdef USE_ACPI_PNP
184         if (device) {
185                 strcat(card->longname, ", bus id ");
186                 strlcat(card->longname, acpi_device_bid(device), sizeof(card->longname));
187         }
188 #endif
189         if (snd_mpu401_uart_new(card, 0,
190 #ifdef CONFIG_X86_PC9800
191                                 pc98ii[dev] ? MPU401_HW_PC98II :
192 #endif
193                                 MPU401_HW_MPU401,
194                                 port[dev], 0,
195                                 irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL) < 0) {
196                 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
197                 snd_card_free(card);
198                 return -ENODEV;
199         }
200         if ((err = snd_card_register(card)) < 0) {
201                 snd_card_free(card);
202                 return err;
203         }
204 #ifdef USE_ACPI_PNP
205         if (device)
206                 acpi_driver_data(device) = card;
207         else
208 #endif
209                 snd_mpu401_legacy_cards[dev] = card;
210         ++cards;
211         return 0;
212 }
213
214 #ifdef USE_ACPI_PNP
215
216 static int __devinit snd_mpu401_acpi_add(struct acpi_device *device)
217 {
218         static int dev;
219         int err;
220
221         for ( ; dev < SNDRV_CARDS; ++dev) {
222                 if (!enable[dev] || !acpipnp[dev])
223                         continue;
224                 err = snd_card_mpu401_probe(dev, device);
225                 if (err < 0)
226                         return err;
227                 ++dev;
228                 return 0;
229         }
230         return -ENODEV;
231 }
232
233 static int __devexit snd_mpu401_acpi_remove(struct acpi_device *device,
234                                             int type)
235 {
236         snd_card_t *card;
237
238         if (!device)
239                 return -EINVAL;
240         card = (snd_card_t *)acpi_driver_data(device);
241         if (!card)
242                 return -EINVAL;
243
244         snd_card_disconnect(card);
245         snd_card_free_in_thread(card);
246         acpi_driver_data(device) = NULL;
247         return 0;
248 }
249
250 static struct acpi_driver snd_mpu401_acpi_driver = {
251         .name = "MPU-401 Driver",
252         .class = "mpu401",
253         .ids = "PNPB006",
254         .ops = {
255                 .add = snd_mpu401_acpi_add,
256                 .remove = __devexit_p(snd_mpu401_acpi_remove),
257         },
258 };
259
260 #endif /* USE_ACPI_PNP */
261
262 static int __init alsa_card_mpu401_init(void)
263 {
264         int dev;
265
266 #ifdef USE_ACPI_PNP
267         if (acpi_bus_register_driver(&snd_mpu401_acpi_driver) >= 0)
268                 acpi_driver_registered = 1;
269 #endif
270         for (dev = 0; dev < SNDRV_CARDS; dev++) {
271                 if (!enable[dev])
272                         continue;
273 #ifdef USE_ACPI_PNP
274                 if (acpipnp[dev] && acpi_driver_registered)
275                         continue;
276 #endif
277                 snd_card_mpu401_probe(dev, NULL);
278         }
279         if (!cards) {
280 #ifdef MODULE
281                 printk(KERN_ERR "MPU-401 device not found or device busy\n");
282 #endif
283 #ifdef USE_ACPI_PNP
284                 if (acpi_driver_registered)
285                         acpi_bus_unregister_driver(&snd_mpu401_acpi_driver);
286 #endif
287                 return -ENODEV;
288         }
289         return 0;
290 }
291
292 static void __exit alsa_card_mpu401_exit(void)
293 {
294         int idx;
295
296 #ifdef USE_ACPI_PNP
297         if (acpi_driver_registered)
298                 acpi_bus_unregister_driver(&snd_mpu401_acpi_driver);
299 #endif
300         for (idx = 0; idx < SNDRV_CARDS; idx++)
301                 snd_card_free(snd_mpu401_legacy_cards[idx]);
302 }
303
304 module_init(alsa_card_mpu401_init)
305 module_exit(alsa_card_mpu401_exit)
306
307 #ifndef MODULE
308
309 /* format is: snd-mpu401=enable,index,id,acpipnp[,pc98ii],port,irq */
310
311 static int __init alsa_card_mpu401_setup(char *str)
312 {
313         static unsigned __initdata nr_dev = 0;
314         int __attribute__ ((__unused__)) pnp = INT_MAX;
315
316         if (nr_dev >= SNDRV_CARDS)
317                 return 0;
318         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
319                get_option(&str,&index[nr_dev]) == 2 &&
320                get_id(&str,&id[nr_dev]) == 2 &&
321                get_option(&str,&pnp) == 2 &&
322 #ifdef CONFIG_X86_PC9800
323                get_option(&str,&pc98ii[nr_dev]) == 2 &&
324 #endif
325                get_option_long(&str,&port[nr_dev]) == 2 &&
326                get_option(&str,&irq[nr_dev]) == 2);
327 #ifdef USE_ACPI_PNP
328         if (pnp != INT_MAX)
329                 acpipnp[nr_dev] = pnp;
330 #endif
331         nr_dev++;
332         return 1;
333 }
334
335 __setup("snd-mpu401=", alsa_card_mpu401_setup);
336
337 #endif /* ifndef MODULE */