source: GPL/trunk/alsa-kernel/drivers/mpu401/mpu401.c

Last change on this file was 777, checked in by David Azarewicz, 7 months ago

Merge from uniaud32-exp branch

File size: 6.8 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
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>
6 */
7
8#include <linux/init.h>
9#include <linux/pnp.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>
16
17MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
18MODULE_DESCRIPTION("MPU-401 UART");
19MODULE_LICENSE("GPL");
20
21#ifndef TARGET_OS2
22static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
23#else
24static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
25#endif
26static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
27static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
28#ifdef CONFIG_PNP
29#ifndef TARGET_OS2
30static bool pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
31#else
32static int pnp[SNDRV_CARDS] = {1,1,1,1,1,1,1,1};
33#endif
34#endif
35static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
36static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
37#ifndef TARGET_OS2
38static bool uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
39#else
40static int uart_enter[SNDRV_CARDS] = {1,1,1,1,1,1,1,1};
41#endif
42
43module_param_array(index, int, NULL, 0444);
44MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
45module_param_array(id, charp, NULL, 0444);
46MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
47module_param_array(enable, bool, NULL, 0444);
48MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
49#ifdef CONFIG_PNP
50module_param_array(pnp, bool, NULL, 0444);
51MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
52#endif
53module_param_hw_array(port, long, ioport, NULL, 0444);
54MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
55module_param_hw_array(irq, int, irq, NULL, 0444);
56MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
57module_param_array(uart_enter, bool, NULL, 0444);
58MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
59
60static struct platform_device *platform_devices[SNDRV_CARDS];
61static int pnp_registered;
62static unsigned int snd_mpu401_devices;
63
64static int snd_mpu401_create(struct device *devptr, int dev,
65 struct snd_card **rcard)
66{
67 struct snd_card *card;
68 int err;
69
70 if (!uart_enter[dev])
71 dev_err(devptr, "the uart_enter option is obsolete; remove it\n");
72
73 *rcard = NULL;
74 err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE,
75 0, &card);
76 if (err < 0)
77 return err;
78 strcpy(card->driver, "MPU-401 UART");
79 strcpy(card->shortname, card->driver);
80 sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
81 if (irq[dev] >= 0) {
82 sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
83 } else {
84 strcat(card->longname, "polled");
85 }
86
87 err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
88 irq[dev], NULL);
89 if (err < 0) {
90 dev_err(devptr, "MPU401 not detected at 0x%lx\n", port[dev]);
91 return err;
92 }
93
94 *rcard = card;
95 return 0;
96}
97
98static int snd_mpu401_probe(struct platform_device *devptr)
99{
100 int dev = devptr->id;
101 int err;
102 struct snd_card *card;
103
104 if (port[dev] == SNDRV_AUTO_PORT) {
105 dev_err(&devptr->dev, "specify port\n");
106 return -EINVAL;
107 }
108 if (irq[dev] == SNDRV_AUTO_IRQ) {
109 dev_err(&devptr->dev, "specify or disable IRQ\n");
110 return -EINVAL;
111 }
112 err = snd_mpu401_create(&devptr->dev, dev, &card);
113 if (err < 0)
114 return err;
115 err = snd_card_register(card);
116 if (err < 0)
117 return err;
118 platform_set_drvdata(devptr, card);
119 return 0;
120}
121
122#define SND_MPU401_DRIVER "snd_mpu401"
123
124static struct platform_driver snd_mpu401_driver = {
125 .probe = snd_mpu401_probe,
126 .driver = {
127 .name = SND_MPU401_DRIVER,
128 },
129};
130
131
132#ifdef CONFIG_PNP
133
134#define IO_EXTENT 2
135
136static const struct pnp_device_id snd_mpu401_pnpids[] = {
137 { .id = "PNPb006" },
138 { .id = "" }
139};
140
141MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
142
143static int snd_mpu401_pnp(int dev, struct pnp_dev *device,
144 const struct pnp_device_id *id)
145{
146 if (!pnp_port_valid(device, 0) ||
147 pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
148 dev_err(&device->dev, "no PnP port\n");
149 return -ENODEV;
150 }
151 if (pnp_port_len(device, 0) < IO_EXTENT) {
152 dev_err(&device->dev, "PnP port length is %llu, expected %d\n",
153 (unsigned long long)pnp_port_len(device, 0),
154 IO_EXTENT);
155 return -ENODEV;
156 }
157 port[dev] = pnp_port_start(device, 0);
158
159 if (!pnp_irq_valid(device, 0) ||
160 pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
161 dev_warn(&device->dev, "no PnP irq, using polling\n");
162 irq[dev] = -1;
163 } else {
164 irq[dev] = pnp_irq(device, 0);
165 }
166 return 0;
167}
168
169static int snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
170 const struct pnp_device_id *id)
171{
172 static int dev;
173 struct snd_card *card;
174 int err;
175
176 for ( ; dev < SNDRV_CARDS; ++dev) {
177 if (!enable[dev] || !pnp[dev])
178 continue;
179 err = snd_mpu401_pnp(dev, pnp_dev, id);
180 if (err < 0)
181 return err;
182 err = snd_mpu401_create(&pnp_dev->dev, dev, &card);
183 if (err < 0)
184 return err;
185 err = snd_card_register(card);
186 if (err < 0)
187 return err;
188 pnp_set_drvdata(pnp_dev, card);
189 snd_mpu401_devices++;
190 ++dev;
191 return 0;
192 }
193 return -ENODEV;
194}
195
196static struct pnp_driver snd_mpu401_pnp_driver = {
197 .name = "mpu401",
198 .id_table = snd_mpu401_pnpids,
199 .probe = snd_mpu401_pnp_probe,
200};
201#else
202static struct pnp_driver snd_mpu401_pnp_driver;
203#endif
204
205static void snd_mpu401_unregister_all(void)
206{
207 int i;
208
209 if (pnp_registered)
210 pnp_unregister_driver(&snd_mpu401_pnp_driver);
211 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
212 platform_device_unregister(platform_devices[i]);
213 platform_driver_unregister(&snd_mpu401_driver);
214}
215
216static int __init alsa_card_mpu401_init(void)
217{
218 int i, err;
219
220 err = platform_driver_register(&snd_mpu401_driver);
221 if (err < 0)
222 return err;
223
224 for (i = 0; i < SNDRV_CARDS; i++) {
225 struct platform_device *device;
226 if (! enable[i])
227 continue;
228#ifdef CONFIG_PNP
229 if (pnp[i])
230 continue;
231#endif
232 device = platform_device_register_simple(SND_MPU401_DRIVER,
233 i, NULL, 0);
234 if (IS_ERR(device))
235 continue;
236 if (!platform_get_drvdata(device)) {
237 platform_device_unregister(device);
238 continue;
239 }
240 platform_devices[i] = device;
241 snd_mpu401_devices++;
242 }
243 err = pnp_register_driver(&snd_mpu401_pnp_driver);
244 if (!err)
245 pnp_registered = 1;
246
247 if (!snd_mpu401_devices) {
248#ifdef MODULE
249 pr_err("MPU-401 device not found or device busy\n");
250#endif
251 snd_mpu401_unregister_all();
252 return -ENODEV;
253 }
254 return 0;
255}
256
257static void __exit alsa_card_mpu401_exit(void)
258{
259 snd_mpu401_unregister_all();
260}
261
262module_init(alsa_card_mpu401_init)
263module_exit(alsa_card_mpu401_exit)
Note: See TracBrowser for help on using the repository browser.