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