blob: 9f36a6472478a3693150856b243d3ef834d9cfb7 [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
44#include <sound/driver.h>
45#include <linux/init.h>
46#include <linux/wait.h>
47#include <linux/sched.h>
Takashi Iwai3564fbb2005-11-17 16:03:26 +010048#include <linux/err.h>
49#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/moduleparam.h>
51#include <sound/core.h>
52#include <sound/seq_kernel.h>
53#include <sound/seq_virmidi.h>
54#include <sound/initval.h>
55
56/* hack: OSS defines midi_devs, so undefine it (versioned symbols) */
57#undef midi_devs
58
59MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
60MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices");
61MODULE_LICENSE("GPL");
62MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual rawmidi device}}");
63
Clemens Ladisch204bdb12005-11-20 14:08:28 +010064#define MAX_MIDI_DEVICES 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
67static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
68static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
69static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
70
71module_param_array(index, int, NULL, 0444);
72MODULE_PARM_DESC(index, "Index value for virmidi soundcard.");
73module_param_array(id, charp, NULL, 0444);
74MODULE_PARM_DESC(id, "ID string for virmidi soundcard.");
75module_param_array(enable, bool, NULL, 0444);
76MODULE_PARM_DESC(enable, "Enable this soundcard.");
77module_param_array(midi_devs, int, NULL, 0444);
Clemens Ladisch204bdb12005-11-20 14:08:28 +010078MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010080struct snd_card_virmidi {
81 struct snd_card *card;
82 struct snd_rawmidi *midi[MAX_MIDI_DEVICES];
83};
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Takashi Iwai3564fbb2005-11-17 16:03:26 +010086static int __init 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
94 sizeof(struct snd_card_virmidi));
95 if (card == NULL)
96 return -ENOMEM;
97 vmidi = (struct snd_card_virmidi *)card->private_data;
98 vmidi->card = card;
99
100 if (midi_devs[dev] > MAX_MIDI_DEVICES) {
101 snd_printk("too much midi devices for virmidi %d: force to use %d\n", dev, MAX_MIDI_DEVICES);
102 midi_devs[dev] = MAX_MIDI_DEVICES;
103 }
104 for (idx = 0; idx < midi_devs[dev]; idx++) {
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100105 struct snd_rawmidi *rmidi;
106 struct snd_virmidi_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if ((err = snd_virmidi_new(card, idx, &rmidi)) < 0)
108 goto __nodev;
109 rdev = rmidi->private_data;
110 vmidi->midi[idx] = rmidi;
111 strcpy(rmidi->name, "Virtual Raw MIDI");
112 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
113 }
114
115 strcpy(card->driver, "VirMIDI");
116 strcpy(card->shortname, "VirMIDI");
117 sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200118
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100119 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if ((err = snd_card_register(card)) == 0) {
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100122 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124 }
125 __nodev:
126 snd_card_free(card);
127 return err;
128}
129
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100130static int snd_virmidi_remove(struct platform_device *devptr)
131{
132 snd_card_free(platform_get_drvdata(devptr));
133 platform_set_drvdata(devptr, NULL);
134 return 0;
135}
136
137#define SND_VIRMIDI_DRIVER "snd_virmidi"
138
139static struct platform_driver snd_virmidi_driver = {
140 .probe = snd_virmidi_probe,
141 .remove = snd_virmidi_remove,
142 .driver = {
143 .name = SND_VIRMIDI_DRIVER
144 },
145};
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int __init alsa_card_virmidi_init(void)
148{
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100149 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100151 if ((err = platform_driver_register(&snd_virmidi_driver)) < 0)
152 return err;
153
154 cards = 0;
155 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
156 struct platform_device *device;
157 device = platform_device_register_simple(SND_VIRMIDI_DRIVER,
158 i, NULL, 0);
159 if (IS_ERR(device)) {
160 err = PTR_ERR(device);
161 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 cards++;
164 }
165 if (!cards) {
166#ifdef MODULE
167 printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
168#endif
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100169 err = -ENODEV;
170 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 return 0;
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100173
174 errout:
175 platform_driver_unregister(&snd_virmidi_driver);
176 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static void __exit alsa_card_virmidi_exit(void)
180{
Takashi Iwai3564fbb2005-11-17 16:03:26 +0100181 platform_driver_unregister(&snd_virmidi_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184module_init(alsa_card_virmidi_init)
185module_exit(alsa_card_virmidi_exit)