| 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 |
|
|---|
| 17 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
|
|---|
| 18 | MODULE_DESCRIPTION("MPU-401 UART");
|
|---|
| 19 | MODULE_LICENSE("GPL");
|
|---|
| 20 |
|
|---|
| 21 | #ifndef TARGET_OS2
|
|---|
| 22 | static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
|
|---|
| 23 | #else
|
|---|
| 24 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
|
|---|
| 25 | #endif
|
|---|
| 26 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
|
|---|
| 27 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
|
|---|
| 28 | #ifdef CONFIG_PNP
|
|---|
| 29 | #ifndef TARGET_OS2
|
|---|
| 30 | static bool pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
|
|---|
| 31 | #else
|
|---|
| 32 | static int pnp[SNDRV_CARDS] = {1,1,1,1,1,1,1,1};
|
|---|
| 33 | #endif
|
|---|
| 34 | #endif
|
|---|
| 35 | static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
|
|---|
| 36 | static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
|
|---|
| 37 | #ifndef TARGET_OS2
|
|---|
| 38 | static bool uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
|
|---|
| 39 | #else
|
|---|
| 40 | static int uart_enter[SNDRV_CARDS] = {1,1,1,1,1,1,1,1};
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | module_param_array(index, int, NULL, 0444);
|
|---|
| 44 | MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
|
|---|
| 45 | module_param_array(id, charp, NULL, 0444);
|
|---|
| 46 | MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
|
|---|
| 47 | module_param_array(enable, bool, NULL, 0444);
|
|---|
| 48 | MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
|
|---|
| 49 | #ifdef CONFIG_PNP
|
|---|
| 50 | module_param_array(pnp, bool, NULL, 0444);
|
|---|
| 51 | MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
|
|---|
| 52 | #endif
|
|---|
| 53 | module_param_hw_array(port, long, ioport, NULL, 0444);
|
|---|
| 54 | MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
|
|---|
| 55 | module_param_hw_array(irq, int, irq, NULL, 0444);
|
|---|
| 56 | MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
|
|---|
| 57 | module_param_array(uart_enter, bool, NULL, 0444);
|
|---|
| 58 | MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
|
|---|
| 59 |
|
|---|
| 60 | static struct platform_device *platform_devices[SNDRV_CARDS];
|
|---|
| 61 | static int pnp_registered;
|
|---|
| 62 | static unsigned int snd_mpu401_devices;
|
|---|
| 63 |
|
|---|
| 64 | static 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 |
|
|---|
| 98 | static 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 |
|
|---|
| 124 | static 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 |
|
|---|
| 136 | static const struct pnp_device_id snd_mpu401_pnpids[] = {
|
|---|
| 137 | { .id = "PNPb006" },
|
|---|
| 138 | { .id = "" }
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
|
|---|
| 142 |
|
|---|
| 143 | static 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 |
|
|---|
| 169 | static 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 |
|
|---|
| 196 | static 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
|
|---|
| 202 | static struct pnp_driver snd_mpu401_pnp_driver;
|
|---|
| 203 | #endif
|
|---|
| 204 |
|
|---|
| 205 | static 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 |
|
|---|
| 216 | static 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 |
|
|---|
| 257 | static void __exit alsa_card_mpu401_exit(void)
|
|---|
| 258 | {
|
|---|
| 259 | snd_mpu401_unregister_all();
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | module_init(alsa_card_mpu401_init)
|
|---|
| 263 | module_exit(alsa_card_mpu401_exit)
|
|---|