1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for generic MPU-401 boards (UART mode only)
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 * Copyright (c) 2004 by Castet Matthieu <castet.matthieu@free.fr>
8 #include <linux/init.h>
10 #include <linux/err.h>
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/mpu401.h>
15 #include <sound/initval.h>
17 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
18 MODULE_DESCRIPTION("MPU-401 UART");
19 MODULE_LICENSE("GPL");
21 static int index
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = -2}; /* exclude the first card */
22 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
23 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE
; /* Enable this card */
25 static bool pnp
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = 1};
27 static long port
[SNDRV_CARDS
] = SNDRV_DEFAULT_PORT
; /* MPU-401 port number */
28 static int irq
[SNDRV_CARDS
] = SNDRV_DEFAULT_IRQ
; /* MPU-401 IRQ */
29 static bool uart_enter
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = 1};
31 module_param_array(index
, int, NULL
, 0444);
32 MODULE_PARM_DESC(index
, "Index value for MPU-401 device.");
33 module_param_array(id
, charp
, NULL
, 0444);
34 MODULE_PARM_DESC(id
, "ID string for MPU-401 device.");
35 module_param_array(enable
, bool, NULL
, 0444);
36 MODULE_PARM_DESC(enable
, "Enable MPU-401 device.");
38 module_param_array(pnp
, bool, NULL
, 0444);
39 MODULE_PARM_DESC(pnp
, "PnP detection for MPU-401 device.");
41 module_param_hw_array(port
, long, ioport
, NULL
, 0444);
42 MODULE_PARM_DESC(port
, "Port # for MPU-401 device.");
43 module_param_hw_array(irq
, int, irq
, NULL
, 0444);
44 MODULE_PARM_DESC(irq
, "IRQ # for MPU-401 device.");
45 module_param_array(uart_enter
, bool, NULL
, 0444);
46 MODULE_PARM_DESC(uart_enter
, "Issue UART_ENTER command at open.");
48 static struct platform_device
*platform_devices
[SNDRV_CARDS
];
49 static int pnp_registered
;
50 static unsigned int snd_mpu401_devices
;
52 static int snd_mpu401_create(struct device
*devptr
, int dev
,
53 struct snd_card
**rcard
)
55 struct snd_card
*card
;
59 snd_printk(KERN_ERR
"the uart_enter option is obsolete; remove it\n");
62 err
= snd_card_new(devptr
, index
[dev
], id
[dev
], THIS_MODULE
,
66 strcpy(card
->driver
, "MPU-401 UART");
67 strcpy(card
->shortname
, card
->driver
);
68 sprintf(card
->longname
, "%s at %#lx, ", card
->shortname
, port
[dev
]);
70 sprintf(card
->longname
+ strlen(card
->longname
), "irq %d", irq
[dev
]);
72 strcat(card
->longname
, "polled");
75 err
= snd_mpu401_uart_new(card
, 0, MPU401_HW_MPU401
, port
[dev
], 0,
78 printk(KERN_ERR
"MPU401 not detected at 0x%lx\n", port
[dev
]);
90 static int snd_mpu401_probe(struct platform_device
*devptr
)
94 struct snd_card
*card
;
96 if (port
[dev
] == SNDRV_AUTO_PORT
) {
97 snd_printk(KERN_ERR
"specify port\n");
100 if (irq
[dev
] == SNDRV_AUTO_IRQ
) {
101 snd_printk(KERN_ERR
"specify or disable IRQ\n");
104 err
= snd_mpu401_create(&devptr
->dev
, dev
, &card
);
107 if ((err
= snd_card_register(card
)) < 0) {
111 platform_set_drvdata(devptr
, card
);
115 static int snd_mpu401_remove(struct platform_device
*devptr
)
117 snd_card_free(platform_get_drvdata(devptr
));
121 #define SND_MPU401_DRIVER "snd_mpu401"
123 static struct platform_driver snd_mpu401_driver
= {
124 .probe
= snd_mpu401_probe
,
125 .remove
= snd_mpu401_remove
,
127 .name
= SND_MPU401_DRIVER
,
136 static const struct pnp_device_id snd_mpu401_pnpids
[] = {
141 MODULE_DEVICE_TABLE(pnp
, snd_mpu401_pnpids
);
143 static int snd_mpu401_pnp(int dev
, struct pnp_dev
*device
,
144 const struct pnp_device_id
*id
)
146 if (!pnp_port_valid(device
, 0) ||
147 pnp_port_flags(device
, 0) & IORESOURCE_DISABLED
) {
148 snd_printk(KERN_ERR
"no PnP port\n");
151 if (pnp_port_len(device
, 0) < IO_EXTENT
) {
152 snd_printk(KERN_ERR
"PnP port length is %llu, expected %d\n",
153 (unsigned long long)pnp_port_len(device
, 0),
157 port
[dev
] = pnp_port_start(device
, 0);
159 if (!pnp_irq_valid(device
, 0) ||
160 pnp_irq_flags(device
, 0) & IORESOURCE_DISABLED
) {
161 snd_printk(KERN_WARNING
"no PnP irq, using polling\n");
164 irq
[dev
] = pnp_irq(device
, 0);
169 static int snd_mpu401_pnp_probe(struct pnp_dev
*pnp_dev
,
170 const struct pnp_device_id
*id
)
173 struct snd_card
*card
;
176 for ( ; dev
< SNDRV_CARDS
; ++dev
) {
177 if (!enable
[dev
] || !pnp
[dev
])
179 err
= snd_mpu401_pnp(dev
, pnp_dev
, id
);
182 err
= snd_mpu401_create(&pnp_dev
->dev
, dev
, &card
);
185 if ((err
= snd_card_register(card
)) < 0) {
189 pnp_set_drvdata(pnp_dev
, card
);
190 snd_mpu401_devices
++;
197 static void snd_mpu401_pnp_remove(struct pnp_dev
*dev
)
199 struct snd_card
*card
= (struct snd_card
*) pnp_get_drvdata(dev
);
201 snd_card_disconnect(card
);
202 snd_card_free_when_closed(card
);
205 static struct pnp_driver snd_mpu401_pnp_driver
= {
207 .id_table
= snd_mpu401_pnpids
,
208 .probe
= snd_mpu401_pnp_probe
,
209 .remove
= snd_mpu401_pnp_remove
,
212 static struct pnp_driver snd_mpu401_pnp_driver
;
215 static void snd_mpu401_unregister_all(void)
220 pnp_unregister_driver(&snd_mpu401_pnp_driver
);
221 for (i
= 0; i
< ARRAY_SIZE(platform_devices
); ++i
)
222 platform_device_unregister(platform_devices
[i
]);
223 platform_driver_unregister(&snd_mpu401_driver
);
226 static int __init
alsa_card_mpu401_init(void)
230 if ((err
= platform_driver_register(&snd_mpu401_driver
)) < 0)
233 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
234 struct platform_device
*device
;
241 device
= platform_device_register_simple(SND_MPU401_DRIVER
,
245 if (!platform_get_drvdata(device
)) {
246 platform_device_unregister(device
);
249 platform_devices
[i
] = device
;
250 snd_mpu401_devices
++;
252 err
= pnp_register_driver(&snd_mpu401_pnp_driver
);
256 if (!snd_mpu401_devices
) {
258 printk(KERN_ERR
"MPU-401 device not found or device busy\n");
260 snd_mpu401_unregister_all();
266 static void __exit
alsa_card_mpu401_exit(void)
268 snd_mpu401_unregister_all();
271 module_init(alsa_card_mpu401_init
)
272 module_exit(alsa_card_mpu401_exit
)