Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Dummy soundcard for virtual rawmidi devices |
| 4 | * |
| 5 | * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * VIRTUAL RAW MIDI DEVICE CARDS |
| 10 | * |
| 11 | * This dummy card contains up to 4 virtual rawmidi devices. |
| 12 | * They are not real rawmidi devices but just associated with sequencer |
| 13 | * clients, so that any input/output sources can be connected as a raw |
| 14 | * MIDI device arbitrary. |
| 15 | * Also, multiple access is allowed to a single rawmidi device. |
| 16 | * |
| 17 | * Typical usage is like following: |
| 18 | * - Load snd-virmidi module. |
| 19 | * # modprobe snd-virmidi index=2 |
| 20 | * Then, sequencer clients 72:0 to 75:0 will be created, which are |
| 21 | * mapped from /dev/snd/midiC1D0 to /dev/snd/midiC1D3, respectively. |
| 22 | * |
| 23 | * - Connect input/output via aconnect. |
| 24 | * % aconnect 64:0 72:0 # keyboard input redirection 64:0 -> 72:0 |
| 25 | * % aconnect 72:0 65:0 # output device redirection 72:0 -> 65:0 |
| 26 | * |
| 27 | * - Run application using a midi device (eg. /dev/snd/midiC1D0) |
| 28 | */ |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/init.h> |
| 31 | #include <linux/wait.h> |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 32 | #include <linux/err.h> |
| 33 | #include <linux/platform_device.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 34 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <sound/core.h> |
| 36 | #include <sound/seq_kernel.h> |
| 37 | #include <sound/seq_virmidi.h> |
| 38 | #include <sound/initval.h> |
| 39 | |
| 40 | /* hack: OSS defines midi_devs, so undefine it (versioned symbols) */ |
| 41 | #undef midi_devs |
| 42 | |
| 43 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); |
| 44 | MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices"); |
| 45 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Clemens Ladisch | 204bdb1 | 2005-11-20 14:08:28 +0100 | [diff] [blame] | 47 | #define MAX_MIDI_DEVICES 4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 50 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 51 | static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4}; |
| 53 | |
| 54 | module_param_array(index, int, NULL, 0444); |
| 55 | MODULE_PARM_DESC(index, "Index value for virmidi soundcard."); |
| 56 | module_param_array(id, charp, NULL, 0444); |
| 57 | MODULE_PARM_DESC(id, "ID string for virmidi soundcard."); |
| 58 | module_param_array(enable, bool, NULL, 0444); |
| 59 | MODULE_PARM_DESC(enable, "Enable this soundcard."); |
| 60 | module_param_array(midi_devs, int, NULL, 0444); |
Clemens Ladisch | 204bdb1 | 2005-11-20 14:08:28 +0100 | [diff] [blame] | 61 | MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 63 | struct snd_card_virmidi { |
| 64 | struct snd_card *card; |
| 65 | struct snd_rawmidi *midi[MAX_MIDI_DEVICES]; |
| 66 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 68 | static struct platform_device *devices[SNDRV_CARDS]; |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 71 | static int snd_virmidi_probe(struct platform_device *devptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 73 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | struct snd_card_virmidi *vmidi; |
| 75 | int idx, err; |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 76 | int dev = devptr->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Takashi Iwai | ed539fc | 2021-07-15 09:59:35 +0200 | [diff] [blame] | 78 | err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, |
| 79 | sizeof(struct snd_card_virmidi), &card); |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 80 | if (err < 0) |
| 81 | return err; |
Joe Perches | 9fe856e | 2010-09-04 18:52:54 -0700 | [diff] [blame] | 82 | vmidi = card->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | vmidi->card = card; |
| 84 | |
| 85 | if (midi_devs[dev] > MAX_MIDI_DEVICES) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 86 | snd_printk(KERN_WARNING |
Kyle Chamberlin | 316638a | 2014-11-28 13:59:56 -0500 | [diff] [blame] | 87 | "too much midi devices for virmidi %d: force to use %d\n", |
| 88 | dev, MAX_MIDI_DEVICES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | midi_devs[dev] = MAX_MIDI_DEVICES; |
| 90 | } |
| 91 | for (idx = 0; idx < midi_devs[dev]; idx++) { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 92 | struct snd_rawmidi *rmidi; |
Kyle Chamberlin | 316638a | 2014-11-28 13:59:56 -0500 | [diff] [blame] | 93 | |
| 94 | err = snd_virmidi_new(card, idx, &rmidi); |
| 95 | if (err < 0) |
Takashi Iwai | ed539fc | 2021-07-15 09:59:35 +0200 | [diff] [blame] | 96 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | vmidi->midi[idx] = rmidi; |
| 98 | strcpy(rmidi->name, "Virtual Raw MIDI"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Kyle Chamberlin | 316638a | 2014-11-28 13:59:56 -0500 | [diff] [blame] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | strcpy(card->driver, "VirMIDI"); |
| 102 | strcpy(card->shortname, "VirMIDI"); |
| 103 | sprintf(card->longname, "Virtual MIDI Card %i", dev + 1); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 104 | |
Kyle Chamberlin | 316638a | 2014-11-28 13:59:56 -0500 | [diff] [blame] | 105 | err = snd_card_register(card); |
Takashi Iwai | ed539fc | 2021-07-15 09:59:35 +0200 | [diff] [blame] | 106 | if (err) |
| 107 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Takashi Iwai | ed539fc | 2021-07-15 09:59:35 +0200 | [diff] [blame] | 109 | platform_set_drvdata(devptr, card); |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | #define SND_VIRMIDI_DRIVER "snd_virmidi" |
| 114 | |
| 115 | static struct platform_driver snd_virmidi_driver = { |
| 116 | .probe = snd_virmidi_probe, |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 117 | .driver = { |
Takashi Iwai | 8bf01d8 | 2012-07-02 10:50:24 +0200 | [diff] [blame] | 118 | .name = SND_VIRMIDI_DRIVER, |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 119 | }, |
| 120 | }; |
| 121 | |
Randy Dunlap | bdec0c7 | 2007-06-25 12:07:38 +0200 | [diff] [blame] | 122 | static void snd_virmidi_unregister_all(void) |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 123 | { |
| 124 | int i; |
| 125 | |
| 126 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
| 127 | platform_device_unregister(devices[i]); |
| 128 | platform_driver_unregister(&snd_virmidi_driver); |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | static int __init alsa_card_virmidi_init(void) |
| 132 | { |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 133 | int i, cards, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Kyle Chamberlin | 316638a | 2014-11-28 13:59:56 -0500 | [diff] [blame] | 135 | err = platform_driver_register(&snd_virmidi_driver); |
| 136 | if (err < 0) |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 137 | return err; |
| 138 | |
| 139 | cards = 0; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 140 | for (i = 0; i < SNDRV_CARDS; i++) { |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 141 | struct platform_device *device; |
Kyle Chamberlin | 316638a | 2014-11-28 13:59:56 -0500 | [diff] [blame] | 142 | |
| 143 | if (!enable[i]) |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 144 | continue; |
Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 145 | device = platform_device_register_simple(SND_VIRMIDI_DRIVER, |
| 146 | i, NULL, 0); |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 147 | if (IS_ERR(device)) |
| 148 | continue; |
Rene Herman | 7152447 | 2006-04-13 12:58:06 +0200 | [diff] [blame] | 149 | if (!platform_get_drvdata(device)) { |
| 150 | platform_device_unregister(device); |
| 151 | continue; |
| 152 | } |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 153 | devices[i] = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | cards++; |
| 155 | } |
| 156 | if (!cards) { |
| 157 | #ifdef MODULE |
| 158 | printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n"); |
| 159 | #endif |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 160 | snd_virmidi_unregister_all(); |
| 161 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void __exit alsa_card_virmidi_exit(void) |
| 167 | { |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 168 | snd_virmidi_unregister_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | module_init(alsa_card_virmidi_init) |
| 172 | module_exit(alsa_card_virmidi_exit) |