| 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., 675 Mass Ave, Cambridge, MA 02139, 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-card-virmidi module. | 
|---|
| 33 | *      # modprobe snd-card-virmidi snd_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 | #define SNDRV_MAIN_OBJECT_FILE | 
|---|
| 45 | #include <sound/driver.h> | 
|---|
| 46 | #include <sound/seq_kernel.h> | 
|---|
| 47 | #include <sound/seq_virmidi.h> | 
|---|
| 48 | #define SNDRV_GET_ID | 
|---|
| 49 | #include <sound/initval.h> | 
|---|
| 50 |  | 
|---|
| 51 | EXPORT_NO_SYMBOLS; | 
|---|
| 52 | MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices"); | 
|---|
| 53 | MODULE_CLASSES("{sound}"); | 
|---|
| 54 | MODULE_DEVICES("{{ALSA,Virtual rawmidi device}}"); | 
|---|
| 55 |  | 
|---|
| 56 | #define MAX_MIDI_DEVICES        8 | 
|---|
| 57 |  | 
|---|
| 58 | static int snd_index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;  /* Index 0-MAX */ | 
|---|
| 59 | static char *snd_id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;   /* ID for this card */ | 
|---|
| 60 | #ifdef TARGET_OS2 | 
|---|
| 61 | static int snd_enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; | 
|---|
| 62 | static int snd_midi_devs[SNDRV_CARDS] = SNDDRV_DEFAULT_MIDI_DEVS | 
|---|
| 63 | #else | 
|---|
| 64 | static int snd_enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; | 
|---|
| 65 | static int snd_midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4}; | 
|---|
| 66 | #endif | 
|---|
| 67 |  | 
|---|
| 68 | MODULE_PARM(snd_index, "1-" __MODULE_STRING(SNDRV_CARDS) "i"); | 
|---|
| 69 | MODULE_PARM_DESC(snd_index, "Index value for virmidi soundcard."); | 
|---|
| 70 | MODULE_PARM_SYNTAX(snd_index, SNDRV_INDEX_DESC); | 
|---|
| 71 | MODULE_PARM(snd_id, "1-" __MODULE_STRING(SNDRV_CARDS) "s"); | 
|---|
| 72 | MODULE_PARM_DESC(snd_id, "ID string for virmidi soundcard."); | 
|---|
| 73 | MODULE_PARM_SYNTAX(snd_id, SNDRV_ID_DESC); | 
|---|
| 74 | MODULE_PARM(snd_enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i"); | 
|---|
| 75 | MODULE_PARM_DESC(snd_enable, "Enable this soundcard."); | 
|---|
| 76 | MODULE_PARM_SYNTAX(snd_enable, SNDRV_ENABLE_DESC); | 
|---|
| 77 | MODULE_PARM(snd_midi_devs, "1-" __MODULE_STRING(SNDRV_CARDS) "i"); | 
|---|
| 78 | MODULE_PARM_DESC(snd_midi_devs, "MIDI devices # (1-8)"); | 
|---|
| 79 | MODULE_PARM_SYNTAX(snd_midi_devs, SNDRV_ENABLED ",allows:{{1,8}}"); | 
|---|
| 80 |  | 
|---|
| 81 | typedef struct snd_card_virmidi { | 
|---|
| 82 | snd_card_t *card; | 
|---|
| 83 | snd_rawmidi_t *midi[MAX_MIDI_DEVICES]; | 
|---|
| 84 | } snd_card_virmidi_t; | 
|---|
| 85 |  | 
|---|
| 86 | static snd_card_t *snd_virmidi_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR; | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | static int __init snd_card_virmidi_probe(int dev) | 
|---|
| 90 | { | 
|---|
| 91 | snd_card_t *card; | 
|---|
| 92 | struct snd_card_virmidi *vmidi; | 
|---|
| 93 | int idx, err; | 
|---|
| 94 |  | 
|---|
| 95 | if (!snd_enable[dev]) | 
|---|
| 96 | return -ENODEV; | 
|---|
| 97 | card = snd_card_new(snd_index[dev], snd_id[dev], THIS_MODULE, | 
|---|
| 98 | sizeof(struct snd_card_virmidi)); | 
|---|
| 99 | if (card == NULL) | 
|---|
| 100 | return -ENOMEM; | 
|---|
| 101 | vmidi = (struct snd_card_virmidi *)card->private_data; | 
|---|
| 102 | vmidi->card = card; | 
|---|
| 103 |  | 
|---|
| 104 | if (snd_midi_devs[dev] > MAX_MIDI_DEVICES) { | 
|---|
| 105 | snd_printk("too much midi devices for virmidi %d: force to use %d\n", dev, MAX_MIDI_DEVICES); | 
|---|
| 106 | snd_midi_devs[dev] = MAX_MIDI_DEVICES; | 
|---|
| 107 | } | 
|---|
| 108 | for (idx = 0; idx < snd_midi_devs[dev]; idx++) { | 
|---|
| 109 | snd_rawmidi_t *rmidi; | 
|---|
| 110 | snd_virmidi_dev_t *rdev; | 
|---|
| 111 | if ((err = snd_virmidi_new(card, idx, &rmidi)) < 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 | if ((err = snd_card_register(card)) == 0) { | 
|---|
| 123 | snd_virmidi_cards[dev] = card; | 
|---|
| 124 | return 0; | 
|---|
| 125 | } | 
|---|
| 126 | __nodev: | 
|---|
| 127 | snd_card_free(card); | 
|---|
| 128 | return err; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | static int __init alsa_card_virmidi_init(void) | 
|---|
| 132 | { | 
|---|
| 133 | int dev, cards; | 
|---|
| 134 |  | 
|---|
| 135 | for (dev = cards = 0; dev < SNDRV_CARDS && snd_enable[dev]; dev++) { | 
|---|
| 136 | if (snd_card_virmidi_probe(dev) < 0) { | 
|---|
| 137 | #ifdef MODULE | 
|---|
| 138 | snd_printk("Card-VirMIDI #%i not found or device busy\n", dev + 1); | 
|---|
| 139 | #endif | 
|---|
| 140 | break; | 
|---|
| 141 | } | 
|---|
| 142 | cards++; | 
|---|
| 143 | } | 
|---|
| 144 | if (!cards) { | 
|---|
| 145 | #ifdef MODULE | 
|---|
| 146 | snd_printk("Card-VirMIDI soundcard not found or device busy\n"); | 
|---|
| 147 | #endif | 
|---|
| 148 | return -ENODEV; | 
|---|
| 149 | } | 
|---|
| 150 | return 0; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | static void __exit alsa_card_virmidi_exit(void) | 
|---|
| 154 | { | 
|---|
| 155 | int dev; | 
|---|
| 156 |  | 
|---|
| 157 | for (dev = 0; dev < SNDRV_CARDS; dev++) | 
|---|
| 158 | snd_card_free(snd_virmidi_cards[dev]); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | module_init(alsa_card_virmidi_init) | 
|---|
| 162 | module_exit(alsa_card_virmidi_exit) | 
|---|
| 163 |  | 
|---|
| 164 | #ifndef MODULE | 
|---|
| 165 |  | 
|---|
| 166 | /* format is: snd-card-virmidi=snd_enable,snd_index,snd_id,snd_midi_devs */ | 
|---|
| 167 |  | 
|---|
| 168 | static int __init alsa_card_virmidi_setup(char *str) | 
|---|
| 169 | { | 
|---|
| 170 | static unsigned __initdata nr_dev = 0; | 
|---|
| 171 |  | 
|---|
| 172 | if (nr_dev >= SNDRV_CARDS) | 
|---|
| 173 | return 0; | 
|---|
| 174 | (void)(get_option(&str,&snd_enable[nr_dev]) == 2 && | 
|---|
| 175 | get_option(&str,&snd_index[nr_dev]) == 2 && | 
|---|
| 176 | get_id(&str,&snd_id[nr_dev]) == 2 && | 
|---|
| 177 | get_option(&str,&snd_midi_devs[nr_dev]) == 2); | 
|---|
| 178 | nr_dev++; | 
|---|
| 179 | return 1; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | __setup("snd-card-virmidi=", alsa_card_virmidi_setup); | 
|---|
| 183 |  | 
|---|
| 184 | #endif /* ifndef MODULE */ | 
|---|