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>
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.
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.
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
23 #include <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/pnp.h>
26 #include <linux/err.h>
27 #include <linux/platform_device.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/mpu401.h>
31 #include <sound/initval.h>
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34 MODULE_DESCRIPTION("MPU-401 UART");
35 MODULE_LICENSE("GPL");
37 static int index
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = -2}; /* exclude the first card */
38 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
39 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE
; /* Enable this card */
41 static int pnp
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = 1};
43 static long port
[SNDRV_CARDS
] = SNDRV_DEFAULT_PORT
; /* MPU-401 port number */
44 static int irq
[SNDRV_CARDS
] = SNDRV_DEFAULT_IRQ
; /* MPU-401 IRQ */
46 module_param_array(index
, int, NULL
, 0444);
47 MODULE_PARM_DESC(index
, "Index value for MPU-401 device.");
48 module_param_array(id
, charp
, NULL
, 0444);
49 MODULE_PARM_DESC(id
, "ID string for MPU-401 device.");
50 module_param_array(enable
, bool, NULL
, 0444);
51 MODULE_PARM_DESC(enable
, "Enable MPU-401 device.");
53 module_param_array(pnp
, bool, NULL
, 0444);
54 MODULE_PARM_DESC(pnp
, "PnP detection for MPU-401 device.");
56 module_param_array(port
, long, NULL
, 0444);
57 MODULE_PARM_DESC(port
, "Port # for MPU-401 device.");
58 module_param_array(irq
, int, NULL
, 0444);
59 MODULE_PARM_DESC(irq
, "IRQ # for MPU-401 device.");
61 static struct platform_device
*platform_devices
[SNDRV_CARDS
];
62 static int pnp_registered
= 0;
64 static int snd_mpu401_create(int dev
, struct snd_card
**rcard
)
66 struct snd_card
*card
;
70 card
= snd_card_new(index
[dev
], id
[dev
], THIS_MODULE
, 0);
73 strcpy(card
->driver
, "MPU-401 UART");
74 strcpy(card
->shortname
, card
->driver
);
75 sprintf(card
->longname
, "%s at %#lx, ", card
->shortname
, port
[dev
]);
77 sprintf(card
->longname
+ strlen(card
->longname
), "irq %d", irq
[dev
]);
79 strcat(card
->longname
, "polled");
82 if ((err
= snd_mpu401_uart_new(card
, 0,
85 irq
[dev
], irq
[dev
] >= 0 ? SA_INTERRUPT
: 0, NULL
)) < 0) {
86 printk(KERN_ERR
"MPU401 not detected at 0x%lx\n", port
[dev
]);
98 static int __devinit
snd_mpu401_probe(struct platform_device
*devptr
)
100 int dev
= devptr
->id
;
102 struct snd_card
*card
;
104 if (port
[dev
] == SNDRV_AUTO_PORT
) {
105 snd_printk(KERN_ERR
"specify port\n");
108 if (irq
[dev
] == SNDRV_AUTO_IRQ
) {
109 snd_printk(KERN_ERR
"specify or disable IRQ\n");
112 err
= snd_mpu401_create(dev
, &card
);
115 snd_card_set_dev(card
, &devptr
->dev
);
116 if ((err
= snd_card_register(card
)) < 0) {
120 platform_set_drvdata(devptr
, card
);
124 static int __devexit
snd_mpu401_remove(struct platform_device
*devptr
)
126 snd_card_free(platform_get_drvdata(devptr
));
127 platform_set_drvdata(devptr
, NULL
);
131 #define SND_MPU401_DRIVER "snd_mpu401"
133 static struct platform_driver snd_mpu401_driver
= {
134 .probe
= snd_mpu401_probe
,
135 .remove
= __devexit_p(snd_mpu401_remove
),
137 .name
= SND_MPU401_DRIVER
146 static struct pnp_device_id snd_mpu401_pnpids
[] = {
151 MODULE_DEVICE_TABLE(pnp
, snd_mpu401_pnpids
);
153 static int __init
snd_mpu401_pnp(int dev
, struct pnp_dev
*device
,
154 const struct pnp_device_id
*id
)
156 if (!pnp_port_valid(device
, 0) ||
157 pnp_port_flags(device
, 0) & IORESOURCE_DISABLED
) {
158 snd_printk(KERN_ERR
"no PnP port\n");
161 if (pnp_port_len(device
, 0) < IO_EXTENT
) {
162 snd_printk(KERN_ERR
"PnP port length is %ld, expected %d\n",
163 pnp_port_len(device
, 0), IO_EXTENT
);
166 port
[dev
] = pnp_port_start(device
, 0);
168 if (!pnp_irq_valid(device
, 0) ||
169 pnp_irq_flags(device
, 0) & IORESOURCE_DISABLED
) {
170 snd_printk(KERN_WARNING
"no PnP irq, using polling\n");
173 irq
[dev
] = pnp_irq(device
, 0);
178 static int __devinit
snd_mpu401_pnp_probe(struct pnp_dev
*pnp_dev
,
179 const struct pnp_device_id
*id
)
182 struct snd_card
*card
;
185 for ( ; dev
< SNDRV_CARDS
; ++dev
) {
186 if (!enable
[dev
] || !pnp
[dev
])
188 err
= snd_mpu401_pnp(dev
, pnp_dev
, id
);
191 err
= snd_mpu401_create(dev
, &card
);
194 if ((err
= snd_card_register(card
)) < 0) {
198 snd_card_set_dev(card
, &pnp_dev
->dev
);
199 pnp_set_drvdata(pnp_dev
, card
);
206 static void __devexit
snd_mpu401_pnp_remove(struct pnp_dev
*dev
)
208 struct snd_card
*card
= (struct snd_card
*) pnp_get_drvdata(dev
);
210 snd_card_disconnect(card
);
211 snd_card_free_in_thread(card
);
214 static struct pnp_driver snd_mpu401_pnp_driver
= {
216 .id_table
= snd_mpu401_pnpids
,
217 .probe
= snd_mpu401_pnp_probe
,
218 .remove
= __devexit_p(snd_mpu401_pnp_remove
),
221 static struct pnp_driver snd_mpu401_pnp_driver
;
224 static void __init_or_module
snd_mpu401_unregister_all(void)
229 pnp_unregister_driver(&snd_mpu401_pnp_driver
);
230 for (i
= 0; i
< ARRAY_SIZE(platform_devices
); ++i
)
231 platform_device_unregister(platform_devices
[i
]);
232 platform_driver_unregister(&snd_mpu401_driver
);
235 static int __init
alsa_card_mpu401_init(void)
239 if ((err
= platform_driver_register(&snd_mpu401_driver
)) < 0)
243 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
244 struct platform_device
*device
;
251 device
= platform_device_register_simple(SND_MPU401_DRIVER
,
253 if (IS_ERR(device
)) {
254 err
= PTR_ERR(device
);
257 platform_devices
[i
] = device
;
260 if ((err
= pnp_register_driver(&snd_mpu401_pnp_driver
)) >= 0) {
267 printk(KERN_ERR
"MPU-401 device not found or device busy\n");
275 snd_mpu401_unregister_all();
279 static void __exit
alsa_card_mpu401_exit(void)
281 snd_mpu401_unregister_all();
284 module_init(alsa_card_mpu401_init
)
285 module_exit(alsa_card_mpu401_exit
)