ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / drivers / virmidi.c
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 <sound/driver.h>
45 #include <linux/init.h>
46 #include <linux/wait.h>
47 #include <linux/sched.h>
48 #include <sound/core.h>
49 #include <sound/seq_kernel.h>
50 #include <sound/seq_virmidi.h>
51 #define SNDRV_GET_ID
52 #include <sound/initval.h>
53
54 /* hack: OSS defines midi_devs, so undefine it (versioned symbols) */
55 #undef midi_devs
56
57 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
58 MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices");
59 MODULE_LICENSE("GPL");
60 MODULE_CLASSES("{sound}");
61 MODULE_DEVICES("{{ALSA,Virtual rawmidi device}}");
62
63 #define MAX_MIDI_DEVICES        8
64
65 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
66 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
67 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
68 static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
69
70 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
71 MODULE_PARM_DESC(index, "Index value for virmidi soundcard.");
72 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
73 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
74 MODULE_PARM_DESC(id, "ID string for virmidi soundcard.");
75 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
76 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
77 MODULE_PARM_DESC(enable, "Enable this soundcard.");
78 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
79 MODULE_PARM(midi_devs, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
80 MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-8)");
81 MODULE_PARM_SYNTAX(midi_devs, SNDRV_ENABLED ",allows:{{1,8}}");
82
83 typedef struct snd_card_virmidi {
84         snd_card_t *card;
85         snd_rawmidi_t *midi[MAX_MIDI_DEVICES];
86 } snd_card_virmidi_t;
87
88 static snd_card_t *snd_virmidi_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
89
90
91 static int __init snd_card_virmidi_probe(int dev)
92 {
93         snd_card_t *card;
94         struct snd_card_virmidi *vmidi;
95         int idx, err;
96
97         if (!enable[dev])
98                 return -ENODEV;
99         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
100                             sizeof(struct snd_card_virmidi));
101         if (card == NULL)
102                 return -ENOMEM;
103         vmidi = (struct snd_card_virmidi *)card->private_data;
104         vmidi->card = card;
105
106         if (midi_devs[dev] > MAX_MIDI_DEVICES) {
107                 snd_printk("too much midi devices for virmidi %d: force to use %d\n", dev, MAX_MIDI_DEVICES);
108                 midi_devs[dev] = MAX_MIDI_DEVICES;
109         }
110         for (idx = 0; idx < midi_devs[dev]; idx++) {
111                 snd_rawmidi_t *rmidi;
112                 snd_virmidi_dev_t *rdev;
113                 if ((err = snd_virmidi_new(card, idx, &rmidi)) < 0)
114                         goto __nodev;
115                 rdev = snd_magic_cast(snd_virmidi_dev_t, rmidi->private_data, continue);
116                 vmidi->midi[idx] = rmidi;
117                 strcpy(rmidi->name, "Virtual Raw MIDI");
118                 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
119         }
120         
121         strcpy(card->driver, "VirMIDI");
122         strcpy(card->shortname, "VirMIDI");
123         sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
124         if ((err = snd_card_register(card)) == 0) {
125                 snd_virmidi_cards[dev] = card;
126                 return 0;
127         }
128       __nodev:
129         snd_card_free(card);
130         return err;
131 }
132
133 static int __init alsa_card_virmidi_init(void)
134 {
135         int dev, cards;
136
137         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
138                 if (snd_card_virmidi_probe(dev) < 0) {
139 #ifdef MODULE
140                         printk(KERN_ERR "Card-VirMIDI #%i not found or device busy\n", dev + 1);
141 #endif
142                         break;
143                 }
144                 cards++;
145         }
146         if (!cards) {
147 #ifdef MODULE
148                 printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
149 #endif
150                 return -ENODEV;
151         }
152         return 0;
153 }
154
155 static void __exit alsa_card_virmidi_exit(void)
156 {
157         int dev;
158
159         for (dev = 0; dev < SNDRV_CARDS; dev++)
160                 snd_card_free(snd_virmidi_cards[dev]);
161 }
162
163 module_init(alsa_card_virmidi_init)
164 module_exit(alsa_card_virmidi_exit)
165
166 #ifndef MODULE
167
168 /* format is: snd-virmidi=enable,index,id,midi_devs */
169
170 static int __init alsa_card_virmidi_setup(char *str)
171 {
172         static unsigned __initdata nr_dev = 0;
173
174         if (nr_dev >= SNDRV_CARDS)
175                 return 0;
176         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
177                get_option(&str,&index[nr_dev]) == 2 &&
178                get_id(&str,&id[nr_dev]) == 2 &&
179                get_option(&str,&midi_devs[nr_dev]) == 2);
180         nr_dev++;
181         return 1;
182 }
183
184 __setup("snd-virmidi=", alsa_card_virmidi_setup);
185
186 #endif /* ifndef MODULE */