blob: f1fb68b15498f28cc22d626b33fc7508483cab52 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Dummy soundcard for virtual rawmidi devices
4 *
5 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
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 Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/init.h>
31#include <linux/wait.h>
Takashi Iwai3564fbb2005-11-17 16:03:26 +010032#include <linux/err.h>
33#include <linux/platform_device.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
43MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
44MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices");
45MODULE_LICENSE("GPL");
46MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual rawmidi device}}");
47
Clemens Ladisch204bdb12005-11-20 14:08:28 +010048#define MAX_MIDI_DEVICES 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
51static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103052static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
54
55module_param_array(index, int, NULL, 0444);
56MODULE_PARM_DESC(index, "Index value for virmidi soundcard.");
57module_param_array(id, charp, NULL, 0444);
58MODULE_PARM_DESC(id, "ID string for virmidi soundcard.");
59module_param_array(enable, bool, NULL, 0444);
60MODULE_PARM_DESC(enable, "Enable this soundcard.");
61module_param_array(midi_devs, int, NULL, 0444);
Clemens Ladisch204bdb12005-11-20 14:08:28 +010062MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010064struct snd_card_virmidi {
65 struct snd_card *card;
66 struct snd_rawmidi *midi[MAX_MIDI_DEVICES];
67};
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Clemens Ladischf7a92752005-12-07 09:13:42 +010069static struct platform_device *devices[SNDRV_CARDS];
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Bill Pembertonfbbb01a2012-12-06 12:35:27 -050072static int snd_virmidi_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010074 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct snd_card_virmidi *vmidi;
76 int idx, err;
Takashi Iwai3564fbb2005-11-17 16:03:26 +010077 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Takashi Iwai5872f3f2014-01-29 12:59:08 +010079 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
80 sizeof(struct snd_card_virmidi), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +010081 if (err < 0)
82 return err;
Joe Perches9fe856e2010-09-04 18:52:54 -070083 vmidi = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 vmidi->card = card;
85
86 if (midi_devs[dev] > MAX_MIDI_DEVICES) {
Takashi Iwai45203832009-02-05 15:51:50 +010087 snd_printk(KERN_WARNING
Kyle Chamberlin316638a2014-11-28 13:59:56 -050088 "too much midi devices for virmidi %d: force to use %d\n",
89 dev, MAX_MIDI_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 midi_devs[dev] = MAX_MIDI_DEVICES;
91 }
92 for (idx = 0; idx < midi_devs[dev]; idx++) {
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010093 struct snd_rawmidi *rmidi;
94 struct snd_virmidi_dev *rdev;
Kyle Chamberlin316638a2014-11-28 13:59:56 -050095
96 err = snd_virmidi_new(card, idx, &rmidi);
97 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 goto __nodev;
99 rdev = rmidi->private_data;
100 vmidi->midi[idx] = rmidi;
101 strcpy(rmidi->name, "Virtual Raw MIDI");
102 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
103 }
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 strcpy(card->driver, "VirMIDI");
106 strcpy(card->shortname, "VirMIDI");
107 sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200108
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500109 err = snd_card_register(card);
Takashi Iwaifc5d3c72014-11-30 20:13:16 +0100110 if (!err) {
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100111 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113 }
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500114__nodev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 snd_card_free(card);
116 return err;
117}
118
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500119static int snd_virmidi_remove(struct platform_device *devptr)
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100120{
121 snd_card_free(platform_get_drvdata(devptr));
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100122 return 0;
123}
124
125#define SND_VIRMIDI_DRIVER "snd_virmidi"
126
127static struct platform_driver snd_virmidi_driver = {
128 .probe = snd_virmidi_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500129 .remove = snd_virmidi_remove,
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100130 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +0200131 .name = SND_VIRMIDI_DRIVER,
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100132 },
133};
134
Randy Dunlapbdec0c72007-06-25 12:07:38 +0200135static void snd_virmidi_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100136{
137 int i;
138
139 for (i = 0; i < ARRAY_SIZE(devices); ++i)
140 platform_device_unregister(devices[i]);
141 platform_driver_unregister(&snd_virmidi_driver);
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int __init alsa_card_virmidi_init(void)
145{
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100146 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500148 err = platform_driver_register(&snd_virmidi_driver);
149 if (err < 0)
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100150 return err;
151
152 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100153 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100154 struct platform_device *device;
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500155
156 if (!enable[i])
Takashi Iwai8278ca82006-02-20 11:57:34 +0100157 continue;
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100158 device = platform_device_register_simple(SND_VIRMIDI_DRIVER,
159 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200160 if (IS_ERR(device))
161 continue;
Rene Herman71524472006-04-13 12:58:06 +0200162 if (!platform_get_drvdata(device)) {
163 platform_device_unregister(device);
164 continue;
165 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100166 devices[i] = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 cards++;
168 }
169 if (!cards) {
170#ifdef MODULE
171 printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
172#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200173 snd_virmidi_unregister_all();
174 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 return 0;
177}
178
179static void __exit alsa_card_virmidi_exit(void)
180{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100181 snd_virmidi_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184module_init(alsa_card_virmidi_init)
185module_exit(alsa_card_virmidi_exit)