0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/init.h>
0009 #include <linux/pnp.h>
0010 #include <linux/err.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/module.h>
0013 #include <sound/core.h>
0014 #include <sound/mpu401.h>
0015 #include <sound/initval.h>
0016
0017 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0018 MODULE_DESCRIPTION("MPU-401 UART");
0019 MODULE_LICENSE("GPL");
0020
0021 static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2};
0022 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0023 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
0024 #ifdef CONFIG_PNP
0025 static bool pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
0026 #endif
0027 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
0028 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
0029 static bool uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
0030
0031 module_param_array(index, int, NULL, 0444);
0032 MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
0033 module_param_array(id, charp, NULL, 0444);
0034 MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
0035 module_param_array(enable, bool, NULL, 0444);
0036 MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
0037 #ifdef CONFIG_PNP
0038 module_param_array(pnp, bool, NULL, 0444);
0039 MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
0040 #endif
0041 module_param_hw_array(port, long, ioport, NULL, 0444);
0042 MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
0043 module_param_hw_array(irq, int, irq, NULL, 0444);
0044 MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
0045 module_param_array(uart_enter, bool, NULL, 0444);
0046 MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
0047
0048 static struct platform_device *platform_devices[SNDRV_CARDS];
0049 static int pnp_registered;
0050 static unsigned int snd_mpu401_devices;
0051
0052 static int snd_mpu401_create(struct device *devptr, int dev,
0053 struct snd_card **rcard)
0054 {
0055 struct snd_card *card;
0056 int err;
0057
0058 if (!uart_enter[dev])
0059 snd_printk(KERN_ERR "the uart_enter option is obsolete; remove it\n");
0060
0061 *rcard = NULL;
0062 err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE,
0063 0, &card);
0064 if (err < 0)
0065 return err;
0066 strcpy(card->driver, "MPU-401 UART");
0067 strcpy(card->shortname, card->driver);
0068 sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
0069 if (irq[dev] >= 0) {
0070 sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
0071 } else {
0072 strcat(card->longname, "polled");
0073 }
0074
0075 err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
0076 irq[dev], NULL);
0077 if (err < 0) {
0078 printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
0079 return err;
0080 }
0081
0082 *rcard = card;
0083 return 0;
0084 }
0085
0086 static int snd_mpu401_probe(struct platform_device *devptr)
0087 {
0088 int dev = devptr->id;
0089 int err;
0090 struct snd_card *card;
0091
0092 if (port[dev] == SNDRV_AUTO_PORT) {
0093 snd_printk(KERN_ERR "specify port\n");
0094 return -EINVAL;
0095 }
0096 if (irq[dev] == SNDRV_AUTO_IRQ) {
0097 snd_printk(KERN_ERR "specify or disable IRQ\n");
0098 return -EINVAL;
0099 }
0100 err = snd_mpu401_create(&devptr->dev, dev, &card);
0101 if (err < 0)
0102 return err;
0103 err = snd_card_register(card);
0104 if (err < 0)
0105 return err;
0106 platform_set_drvdata(devptr, card);
0107 return 0;
0108 }
0109
0110 #define SND_MPU401_DRIVER "snd_mpu401"
0111
0112 static struct platform_driver snd_mpu401_driver = {
0113 .probe = snd_mpu401_probe,
0114 .driver = {
0115 .name = SND_MPU401_DRIVER,
0116 },
0117 };
0118
0119
0120 #ifdef CONFIG_PNP
0121
0122 #define IO_EXTENT 2
0123
0124 static const struct pnp_device_id snd_mpu401_pnpids[] = {
0125 { .id = "PNPb006" },
0126 { .id = "" }
0127 };
0128
0129 MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
0130
0131 static int snd_mpu401_pnp(int dev, struct pnp_dev *device,
0132 const struct pnp_device_id *id)
0133 {
0134 if (!pnp_port_valid(device, 0) ||
0135 pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
0136 snd_printk(KERN_ERR "no PnP port\n");
0137 return -ENODEV;
0138 }
0139 if (pnp_port_len(device, 0) < IO_EXTENT) {
0140 snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n",
0141 (unsigned long long)pnp_port_len(device, 0),
0142 IO_EXTENT);
0143 return -ENODEV;
0144 }
0145 port[dev] = pnp_port_start(device, 0);
0146
0147 if (!pnp_irq_valid(device, 0) ||
0148 pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
0149 snd_printk(KERN_WARNING "no PnP irq, using polling\n");
0150 irq[dev] = -1;
0151 } else {
0152 irq[dev] = pnp_irq(device, 0);
0153 }
0154 return 0;
0155 }
0156
0157 static int snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
0158 const struct pnp_device_id *id)
0159 {
0160 static int dev;
0161 struct snd_card *card;
0162 int err;
0163
0164 for ( ; dev < SNDRV_CARDS; ++dev) {
0165 if (!enable[dev] || !pnp[dev])
0166 continue;
0167 err = snd_mpu401_pnp(dev, pnp_dev, id);
0168 if (err < 0)
0169 return err;
0170 err = snd_mpu401_create(&pnp_dev->dev, dev, &card);
0171 if (err < 0)
0172 return err;
0173 err = snd_card_register(card);
0174 if (err < 0)
0175 return err;
0176 pnp_set_drvdata(pnp_dev, card);
0177 snd_mpu401_devices++;
0178 ++dev;
0179 return 0;
0180 }
0181 return -ENODEV;
0182 }
0183
0184 static struct pnp_driver snd_mpu401_pnp_driver = {
0185 .name = "mpu401",
0186 .id_table = snd_mpu401_pnpids,
0187 .probe = snd_mpu401_pnp_probe,
0188 };
0189 #else
0190 static struct pnp_driver snd_mpu401_pnp_driver;
0191 #endif
0192
0193 static void snd_mpu401_unregister_all(void)
0194 {
0195 int i;
0196
0197 if (pnp_registered)
0198 pnp_unregister_driver(&snd_mpu401_pnp_driver);
0199 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
0200 platform_device_unregister(platform_devices[i]);
0201 platform_driver_unregister(&snd_mpu401_driver);
0202 }
0203
0204 static int __init alsa_card_mpu401_init(void)
0205 {
0206 int i, err;
0207
0208 err = platform_driver_register(&snd_mpu401_driver);
0209 if (err < 0)
0210 return err;
0211
0212 for (i = 0; i < SNDRV_CARDS; i++) {
0213 struct platform_device *device;
0214 if (! enable[i])
0215 continue;
0216 #ifdef CONFIG_PNP
0217 if (pnp[i])
0218 continue;
0219 #endif
0220 device = platform_device_register_simple(SND_MPU401_DRIVER,
0221 i, NULL, 0);
0222 if (IS_ERR(device))
0223 continue;
0224 if (!platform_get_drvdata(device)) {
0225 platform_device_unregister(device);
0226 continue;
0227 }
0228 platform_devices[i] = device;
0229 snd_mpu401_devices++;
0230 }
0231 err = pnp_register_driver(&snd_mpu401_pnp_driver);
0232 if (!err)
0233 pnp_registered = 1;
0234
0235 if (!snd_mpu401_devices) {
0236 #ifdef MODULE
0237 printk(KERN_ERR "MPU-401 device not found or device busy\n");
0238 #endif
0239 snd_mpu401_unregister_all();
0240 return -ENODEV;
0241 }
0242 return 0;
0243 }
0244
0245 static void __exit alsa_card_mpu401_exit(void)
0246 {
0247 snd_mpu401_unregister_all();
0248 }
0249
0250 module_init(alsa_card_mpu401_init)
0251 module_exit(alsa_card_mpu401_exit)