| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 47 | #include <linux/err.h> | 
 | 48 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/moduleparam.h> | 
 | 50 | #include <sound/core.h> | 
 | 51 | #include <sound/seq_kernel.h> | 
 | 52 | #include <sound/seq_virmidi.h> | 
 | 53 | #include <sound/initval.h> | 
 | 54 |  | 
 | 55 | /* hack: OSS defines midi_devs, so undefine it (versioned symbols) */ | 
 | 56 | #undef midi_devs | 
 | 57 |  | 
 | 58 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); | 
 | 59 | MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices"); | 
 | 60 | MODULE_LICENSE("GPL"); | 
 | 61 | MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual rawmidi device}}"); | 
 | 62 |  | 
| Clemens Ladisch | 204bdb1 | 2005-11-20 14:08:28 +0100 | [diff] [blame] | 63 | #define MAX_MIDI_DEVICES	4 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ | 
| Clemens Ladisch | c5533bf | 2006-06-02 09:15:44 +0200 | [diff] [blame] | 67 | static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4}; | 
 | 69 |  | 
 | 70 | module_param_array(index, int, NULL, 0444); | 
 | 71 | MODULE_PARM_DESC(index, "Index value for virmidi soundcard."); | 
 | 72 | module_param_array(id, charp, NULL, 0444); | 
 | 73 | MODULE_PARM_DESC(id, "ID string for virmidi soundcard."); | 
 | 74 | module_param_array(enable, bool, NULL, 0444); | 
 | 75 | MODULE_PARM_DESC(enable, "Enable this soundcard."); | 
 | 76 | module_param_array(midi_devs, int, NULL, 0444); | 
| Clemens Ladisch | 204bdb1 | 2005-11-20 14:08:28 +0100 | [diff] [blame] | 77 | MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 |  | 
| Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 79 | struct snd_card_virmidi { | 
 | 80 | 	struct snd_card *card; | 
 | 81 | 	struct snd_rawmidi *midi[MAX_MIDI_DEVICES]; | 
 | 82 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 |  | 
| Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 84 | static struct platform_device *devices[SNDRV_CARDS]; | 
 | 85 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
| Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 87 | static int __devinit snd_virmidi_probe(struct platform_device *devptr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { | 
| Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 89 | 	struct snd_card *card; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | 	struct snd_card_virmidi *vmidi; | 
 | 91 | 	int idx, err; | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 92 | 	int dev = devptr->id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, | 
 | 95 | 			    sizeof(struct snd_card_virmidi)); | 
 | 96 | 	if (card == NULL) | 
 | 97 | 		return -ENOMEM; | 
 | 98 | 	vmidi = (struct snd_card_virmidi *)card->private_data; | 
 | 99 | 	vmidi->card = card; | 
 | 100 |  | 
 | 101 | 	if (midi_devs[dev] > MAX_MIDI_DEVICES) { | 
 | 102 | 		snd_printk("too much midi devices for virmidi %d: force to use %d\n", dev, MAX_MIDI_DEVICES); | 
 | 103 | 		midi_devs[dev] = MAX_MIDI_DEVICES; | 
 | 104 | 	} | 
 | 105 | 	for (idx = 0; idx < midi_devs[dev]; idx++) { | 
| Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 106 | 		struct snd_rawmidi *rmidi; | 
 | 107 | 		struct snd_virmidi_dev *rdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | 		if ((err = snd_virmidi_new(card, idx, &rmidi)) < 0) | 
 | 109 | 			goto __nodev; | 
 | 110 | 		rdev = rmidi->private_data; | 
 | 111 | 		vmidi->midi[idx] = rmidi; | 
 | 112 | 		strcpy(rmidi->name, "Virtual Raw MIDI"); | 
 | 113 | 		rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH; | 
 | 114 | 	} | 
 | 115 | 	 | 
 | 116 | 	strcpy(card->driver, "VirMIDI"); | 
 | 117 | 	strcpy(card->shortname, "VirMIDI"); | 
 | 118 | 	sprintf(card->longname, "Virtual MIDI Card %i", dev + 1); | 
| Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 119 |  | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 120 | 	snd_card_set_dev(card, &devptr->dev); | 
| Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 121 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | 	if ((err = snd_card_register(card)) == 0) { | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 123 | 		platform_set_drvdata(devptr, card); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 		return 0; | 
 | 125 | 	} | 
 | 126 |       __nodev: | 
 | 127 | 	snd_card_free(card); | 
 | 128 | 	return err; | 
 | 129 | } | 
 | 130 |  | 
| Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 131 | static int __devexit snd_virmidi_remove(struct platform_device *devptr) | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 132 | { | 
 | 133 | 	snd_card_free(platform_get_drvdata(devptr)); | 
 | 134 | 	platform_set_drvdata(devptr, NULL); | 
 | 135 | 	return 0; | 
 | 136 | } | 
 | 137 |  | 
 | 138 | #define SND_VIRMIDI_DRIVER	"snd_virmidi" | 
 | 139 |  | 
 | 140 | static struct platform_driver snd_virmidi_driver = { | 
 | 141 | 	.probe		= snd_virmidi_probe, | 
| Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 142 | 	.remove		= __devexit_p(snd_virmidi_remove), | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 143 | 	.driver		= { | 
 | 144 | 		.name	= SND_VIRMIDI_DRIVER | 
 | 145 | 	}, | 
 | 146 | }; | 
 | 147 |  | 
| Randy Dunlap | bdec0c7 | 2007-06-25 12:07:38 +0200 | [diff] [blame] | 148 | static void snd_virmidi_unregister_all(void) | 
| Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 149 | { | 
 | 150 | 	int i; | 
 | 151 |  | 
 | 152 | 	for (i = 0; i < ARRAY_SIZE(devices); ++i) | 
 | 153 | 		platform_device_unregister(devices[i]); | 
 | 154 | 	platform_driver_unregister(&snd_virmidi_driver); | 
 | 155 | } | 
 | 156 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static int __init alsa_card_virmidi_init(void) | 
 | 158 | { | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 159 | 	int i, cards, err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 161 | 	if ((err = platform_driver_register(&snd_virmidi_driver)) < 0) | 
 | 162 | 		return err; | 
 | 163 |  | 
 | 164 | 	cards = 0; | 
| Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 165 | 	for (i = 0; i < SNDRV_CARDS; i++) { | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 166 | 		struct platform_device *device; | 
| Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 167 | 		if (! enable[i]) | 
 | 168 | 			continue; | 
| Takashi Iwai | 3564fbb | 2005-11-17 16:03:26 +0100 | [diff] [blame] | 169 | 		device = platform_device_register_simple(SND_VIRMIDI_DRIVER, | 
 | 170 | 							 i, NULL, 0); | 
| Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 171 | 		if (IS_ERR(device)) | 
 | 172 | 			continue; | 
| Rene Herman | 7152447 | 2006-04-13 12:58:06 +0200 | [diff] [blame] | 173 | 		if (!platform_get_drvdata(device)) { | 
 | 174 | 			platform_device_unregister(device); | 
 | 175 | 			continue; | 
 | 176 | 		} | 
| Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 177 | 		devices[i] = device; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | 		cards++; | 
 | 179 | 	} | 
 | 180 | 	if (!cards) { | 
 | 181 | #ifdef MODULE | 
 | 182 | 		printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n"); | 
 | 183 | #endif | 
| Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 184 | 		snd_virmidi_unregister_all(); | 
 | 185 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | 	} | 
 | 187 | 	return 0; | 
 | 188 | } | 
 | 189 |  | 
 | 190 | static void __exit alsa_card_virmidi_exit(void) | 
 | 191 | { | 
| Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 192 | 	snd_virmidi_unregister_all(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } | 
 | 194 |  | 
 | 195 | module_init(alsa_card_virmidi_init) | 
 | 196 | module_exit(alsa_card_virmidi_exit) |