blob: 33ef13a72e69aa9203cf6496e9a45c39bc135a2c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/init.h>
45#include <linux/wait.h>
Takashi Iwai3564fbb2005-11-17 16:03:26 +010046#include <linux/err.h>
47#include <linux/platform_device.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040048#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#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
Clemens Ladisch204bdb12005-11-20 14:08:28 +010062#define MAX_MIDI_DEVICES 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
65static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103066static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static 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);
Clemens Ladisch204bdb12005-11-20 14:08:28 +010076MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010078struct snd_card_virmidi {
79 struct snd_card *card;
80 struct snd_rawmidi *midi[MAX_MIDI_DEVICES];
81};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Clemens Ladischf7a92752005-12-07 09:13:42 +010083static struct platform_device *devices[SNDRV_CARDS];
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Bill Pembertonfbbb01a2012-12-06 12:35:27 -050086static int snd_virmidi_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010088 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct snd_card_virmidi *vmidi;
90 int idx, err;
Takashi Iwai3564fbb2005-11-17 16:03:26 +010091 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Takashi Iwai5872f3f2014-01-29 12:59:08 +010093 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
94 sizeof(struct snd_card_virmidi), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +010095 if (err < 0)
96 return err;
Joe Perches9fe856e2010-09-04 18:52:54 -070097 vmidi = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 vmidi->card = card;
99
100 if (midi_devs[dev] > MAX_MIDI_DEVICES) {
Takashi Iwai45203832009-02-05 15:51:50 +0100101 snd_printk(KERN_WARNING
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500102 "too much midi devices for virmidi %d: force to use %d\n",
103 dev, MAX_MIDI_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 midi_devs[dev] = MAX_MIDI_DEVICES;
105 }
106 for (idx = 0; idx < midi_devs[dev]; idx++) {
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100107 struct snd_rawmidi *rmidi;
108 struct snd_virmidi_dev *rdev;
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500109
110 err = snd_virmidi_new(card, idx, &rmidi);
111 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 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 }
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 strcpy(card->driver, "VirMIDI");
120 strcpy(card->shortname, "VirMIDI");
121 sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200122
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500123 err = snd_card_register(card);
Takashi Iwaifc5d3c72014-11-30 20:13:16 +0100124 if (!err) {
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100125 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127 }
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500128__nodev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 snd_card_free(card);
130 return err;
131}
132
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500133static int snd_virmidi_remove(struct platform_device *devptr)
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100134{
135 snd_card_free(platform_get_drvdata(devptr));
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100136 return 0;
137}
138
139#define SND_VIRMIDI_DRIVER "snd_virmidi"
140
141static struct platform_driver snd_virmidi_driver = {
142 .probe = snd_virmidi_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500143 .remove = snd_virmidi_remove,
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100144 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +0200145 .name = SND_VIRMIDI_DRIVER,
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100146 },
147};
148
Randy Dunlapbdec0c72007-06-25 12:07:38 +0200149static void snd_virmidi_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100150{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int __init alsa_card_virmidi_init(void)
159{
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100160 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500162 err = platform_driver_register(&snd_virmidi_driver);
163 if (err < 0)
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100164 return err;
165
166 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100167 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100168 struct platform_device *device;
Kyle Chamberlin316638a2014-11-28 13:59:56 -0500169
170 if (!enable[i])
Takashi Iwai8278ca82006-02-20 11:57:34 +0100171 continue;
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100172 device = platform_device_register_simple(SND_VIRMIDI_DRIVER,
173 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200174 if (IS_ERR(device))
175 continue;
Rene Herman71524472006-04-13 12:58:06 +0200176 if (!platform_get_drvdata(device)) {
177 platform_device_unregister(device);
178 continue;
179 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100180 devices[i] = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 cards++;
182 }
183 if (!cards) {
184#ifdef MODULE
185 printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
186#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200187 snd_virmidi_unregister_all();
188 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 return 0;
191}
192
193static void __exit alsa_card_virmidi_exit(void)
194{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100195 snd_virmidi_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198module_init(alsa_card_virmidi_init)
199module_exit(alsa_card_virmidi_exit)