Loading...
1/*
2 * Dummy soundcard for virtual rawmidi devices
3 *
4 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23 * VIRTUAL RAW MIDI DEVICE CARDS
24 *
25 * This dummy card contains up to 4 virtual rawmidi devices.
26 * They are not real rawmidi devices but just associated with sequencer
27 * clients, so that any input/output sources can be connected as a raw
28 * MIDI device arbitrary.
29 * Also, multiple access is allowed to a single rawmidi device.
30 *
31 * Typical usage is like following:
32 * - Load snd-virmidi module.
33 * # modprobe snd-virmidi index=2
34 * Then, sequencer clients 72:0 to 75:0 will be created, which are
35 * mapped from /dev/snd/midiC1D0 to /dev/snd/midiC1D3, respectively.
36 *
37 * - Connect input/output via aconnect.
38 * % aconnect 64:0 72:0 # keyboard input redirection 64:0 -> 72:0
39 * % aconnect 72:0 65:0 # output device redirection 72:0 -> 65:0
40 *
41 * - Run application using a midi device (eg. /dev/snd/midiC1D0)
42 */
43
44#include <linux/init.h>
45#include <linux/wait.h>
46#include <linux/err.h>
47#include <linux/platform_device.h>
48#include <linux/module.h>
49#include <sound/core.h>
50#include <sound/seq_kernel.h>
51#include <sound/seq_virmidi.h>
52#include <sound/initval.h>
53
54/* hack: OSS defines midi_devs, so undefine it (versioned symbols) */
55#undef midi_devs
56
57MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
58MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual rawmidi device}}");
61
62#define MAX_MIDI_DEVICES 4
63
64static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
65static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
66static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
67static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
68
69module_param_array(index, int, NULL, 0444);
70MODULE_PARM_DESC(index, "Index value for virmidi soundcard.");
71module_param_array(id, charp, NULL, 0444);
72MODULE_PARM_DESC(id, "ID string for virmidi soundcard.");
73module_param_array(enable, bool, NULL, 0444);
74MODULE_PARM_DESC(enable, "Enable this soundcard.");
75module_param_array(midi_devs, int, NULL, 0444);
76MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)");
77
78struct snd_card_virmidi {
79 struct snd_card *card;
80 struct snd_rawmidi *midi[MAX_MIDI_DEVICES];
81};
82
83static struct platform_device *devices[SNDRV_CARDS];
84
85
86static int snd_virmidi_probe(struct platform_device *devptr)
87{
88 struct snd_card *card;
89 struct snd_card_virmidi *vmidi;
90 int idx, err;
91 int dev = devptr->id;
92
93 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
94 sizeof(struct snd_card_virmidi), &card);
95 if (err < 0)
96 return err;
97 vmidi = card->private_data;
98 vmidi->card = card;
99
100 if (midi_devs[dev] > MAX_MIDI_DEVICES) {
101 snd_printk(KERN_WARNING
102 "too much midi devices for virmidi %d: force to use %d\n",
103 dev, MAX_MIDI_DEVICES);
104 midi_devs[dev] = MAX_MIDI_DEVICES;
105 }
106 for (idx = 0; idx < midi_devs[dev]; idx++) {
107 struct snd_rawmidi *rmidi;
108 struct snd_virmidi_dev *rdev;
109
110 err = snd_virmidi_new(card, idx, &rmidi);
111 if (err < 0)
112 goto __nodev;
113 rdev = rmidi->private_data;
114 vmidi->midi[idx] = rmidi;
115 strcpy(rmidi->name, "Virtual Raw MIDI");
116 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
117 }
118
119 strcpy(card->driver, "VirMIDI");
120 strcpy(card->shortname, "VirMIDI");
121 sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
122
123 err = snd_card_register(card);
124 if (!err) {
125 platform_set_drvdata(devptr, card);
126 return 0;
127 }
128__nodev:
129 snd_card_free(card);
130 return err;
131}
132
133static int snd_virmidi_remove(struct platform_device *devptr)
134{
135 snd_card_free(platform_get_drvdata(devptr));
136 return 0;
137}
138
139#define SND_VIRMIDI_DRIVER "snd_virmidi"
140
141static struct platform_driver snd_virmidi_driver = {
142 .probe = snd_virmidi_probe,
143 .remove = snd_virmidi_remove,
144 .driver = {
145 .name = SND_VIRMIDI_DRIVER,
146 },
147};
148
149static void snd_virmidi_unregister_all(void)
150{
151 int i;
152
153 for (i = 0; i < ARRAY_SIZE(devices); ++i)
154 platform_device_unregister(devices[i]);
155 platform_driver_unregister(&snd_virmidi_driver);
156}
157
158static int __init alsa_card_virmidi_init(void)
159{
160 int i, cards, err;
161
162 err = platform_driver_register(&snd_virmidi_driver);
163 if (err < 0)
164 return err;
165
166 cards = 0;
167 for (i = 0; i < SNDRV_CARDS; i++) {
168 struct platform_device *device;
169
170 if (!enable[i])
171 continue;
172 device = platform_device_register_simple(SND_VIRMIDI_DRIVER,
173 i, NULL, 0);
174 if (IS_ERR(device))
175 continue;
176 if (!platform_get_drvdata(device)) {
177 platform_device_unregister(device);
178 continue;
179 }
180 devices[i] = device;
181 cards++;
182 }
183 if (!cards) {
184#ifdef MODULE
185 printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
186#endif
187 snd_virmidi_unregister_all();
188 return -ENODEV;
189 }
190 return 0;
191}
192
193static void __exit alsa_card_virmidi_exit(void)
194{
195 snd_virmidi_unregister_all();
196}
197
198module_init(alsa_card_virmidi_init)
199module_exit(alsa_card_virmidi_exit)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Dummy soundcard for virtual rawmidi devices
4 *
5 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
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
30#include <linux/init.h>
31#include <linux/wait.h>
32#include <linux/err.h>
33#include <linux/platform_device.h>
34#include <linux/module.h>
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
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
48#define MAX_MIDI_DEVICES 4
49
50static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
51static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
52static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
53static 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);
62MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)");
63
64struct snd_card_virmidi {
65 struct snd_card *card;
66 struct snd_rawmidi *midi[MAX_MIDI_DEVICES];
67};
68
69static struct platform_device *devices[SNDRV_CARDS];
70
71
72static int snd_virmidi_probe(struct platform_device *devptr)
73{
74 struct snd_card *card;
75 struct snd_card_virmidi *vmidi;
76 int idx, err;
77 int dev = devptr->id;
78
79 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
80 sizeof(struct snd_card_virmidi), &card);
81 if (err < 0)
82 return err;
83 vmidi = card->private_data;
84 vmidi->card = card;
85
86 if (midi_devs[dev] > MAX_MIDI_DEVICES) {
87 snd_printk(KERN_WARNING
88 "too much midi devices for virmidi %d: force to use %d\n",
89 dev, MAX_MIDI_DEVICES);
90 midi_devs[dev] = MAX_MIDI_DEVICES;
91 }
92 for (idx = 0; idx < midi_devs[dev]; idx++) {
93 struct snd_rawmidi *rmidi;
94 struct snd_virmidi_dev *rdev;
95
96 err = snd_virmidi_new(card, idx, &rmidi);
97 if (err < 0)
98 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 }
104
105 strcpy(card->driver, "VirMIDI");
106 strcpy(card->shortname, "VirMIDI");
107 sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
108
109 err = snd_card_register(card);
110 if (!err) {
111 platform_set_drvdata(devptr, card);
112 return 0;
113 }
114__nodev:
115 snd_card_free(card);
116 return err;
117}
118
119static int snd_virmidi_remove(struct platform_device *devptr)
120{
121 snd_card_free(platform_get_drvdata(devptr));
122 return 0;
123}
124
125#define SND_VIRMIDI_DRIVER "snd_virmidi"
126
127static struct platform_driver snd_virmidi_driver = {
128 .probe = snd_virmidi_probe,
129 .remove = snd_virmidi_remove,
130 .driver = {
131 .name = SND_VIRMIDI_DRIVER,
132 },
133};
134
135static void snd_virmidi_unregister_all(void)
136{
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
144static int __init alsa_card_virmidi_init(void)
145{
146 int i, cards, err;
147
148 err = platform_driver_register(&snd_virmidi_driver);
149 if (err < 0)
150 return err;
151
152 cards = 0;
153 for (i = 0; i < SNDRV_CARDS; i++) {
154 struct platform_device *device;
155
156 if (!enable[i])
157 continue;
158 device = platform_device_register_simple(SND_VIRMIDI_DRIVER,
159 i, NULL, 0);
160 if (IS_ERR(device))
161 continue;
162 if (!platform_get_drvdata(device)) {
163 platform_device_unregister(device);
164 continue;
165 }
166 devices[i] = device;
167 cards++;
168 }
169 if (!cards) {
170#ifdef MODULE
171 printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
172#endif
173 snd_virmidi_unregister_all();
174 return -ENODEV;
175 }
176 return 0;
177}
178
179static void __exit alsa_card_virmidi_exit(void)
180{
181 snd_virmidi_unregister_all();
182}
183
184module_init(alsa_card_virmidi_init)
185module_exit(alsa_card_virmidi_exit)