virmidi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * VIRTUAL RAW MIDI DEVICE CARDS
  23. *
  24. * This dummy card contains up to 4 virtual rawmidi devices.
  25. * They are not real rawmidi devices but just associated with sequencer
  26. * clients, so that any input/output sources can be connected as a raw
  27. * MIDI device arbitrary.
  28. * Also, multiple access is allowed to a single rawmidi device.
  29. *
  30. * Typical usage is like following:
  31. * - Load snd-virmidi module.
  32. * # modprobe snd-virmidi index=2
  33. * Then, sequencer clients 72:0 to 75:0 will be created, which are
  34. * mapped from /dev/snd/midiC1D0 to /dev/snd/midiC1D3, respectively.
  35. *
  36. * - Connect input/output via aconnect.
  37. * % aconnect 64:0 72:0 # keyboard input redirection 64:0 -> 72:0
  38. * % aconnect 72:0 65:0 # output device redirection 72:0 -> 65:0
  39. *
  40. * - Run application using a midi device (eg. /dev/snd/midiC1D0)
  41. */
  42. #include <sound/driver.h>
  43. #include <linux/init.h>
  44. #include <linux/wait.h>
  45. #include <linux/err.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/moduleparam.h>
  48. #include <sound/core.h>
  49. #include <sound/seq_kernel.h>
  50. #include <sound/seq_virmidi.h>
  51. #include <sound/initval.h>
  52. /* hack: OSS defines midi_devs, so undefine it (versioned symbols) */
  53. #undef midi_devs
  54. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  55. MODULE_DESCRIPTION("Dummy soundcard for virtual rawmidi devices");
  56. MODULE_LICENSE("GPL");
  57. MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual rawmidi device}}");
  58. #define MAX_MIDI_DEVICES 4
  59. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  60. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  61. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  62. static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
  63. module_param_array(index, int, NULL, 0444);
  64. MODULE_PARM_DESC(index, "Index value for virmidi soundcard.");
  65. module_param_array(id, charp, NULL, 0444);
  66. MODULE_PARM_DESC(id, "ID string for virmidi soundcard.");
  67. module_param_array(enable, bool, NULL, 0444);
  68. MODULE_PARM_DESC(enable, "Enable this soundcard.");
  69. module_param_array(midi_devs, int, NULL, 0444);
  70. MODULE_PARM_DESC(midi_devs, "MIDI devices # (1-4)");
  71. struct snd_card_virmidi {
  72. struct snd_card *card;
  73. struct snd_rawmidi *midi[MAX_MIDI_DEVICES];
  74. };
  75. static struct platform_device *devices[SNDRV_CARDS];
  76. static int __devinit snd_virmidi_probe(struct platform_device *devptr)
  77. {
  78. struct snd_card *card;
  79. struct snd_card_virmidi *vmidi;
  80. int idx, err;
  81. int dev = devptr->id;
  82. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  83. sizeof(struct snd_card_virmidi));
  84. if (card == NULL)
  85. return -ENOMEM;
  86. vmidi = (struct snd_card_virmidi *)card->private_data;
  87. vmidi->card = card;
  88. if (midi_devs[dev] > MAX_MIDI_DEVICES) {
  89. snd_printk("too much midi devices for virmidi %d: force to use %d\n", dev, MAX_MIDI_DEVICES);
  90. midi_devs[dev] = MAX_MIDI_DEVICES;
  91. }
  92. for (idx = 0; idx < midi_devs[dev]; idx++) {
  93. struct snd_rawmidi *rmidi;
  94. struct snd_virmidi_dev *rdev;
  95. if ((err = snd_virmidi_new(card, idx, &rmidi)) < 0)
  96. goto __nodev;
  97. rdev = rmidi->private_data;
  98. vmidi->midi[idx] = rmidi;
  99. strcpy(rmidi->name, "Virtual Raw MIDI");
  100. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
  101. }
  102. strcpy(card->driver, "VirMIDI");
  103. strcpy(card->shortname, "VirMIDI");
  104. sprintf(card->longname, "Virtual MIDI Card %i", dev + 1);
  105. snd_card_set_dev(card, &devptr->dev);
  106. if ((err = snd_card_register(card)) == 0) {
  107. platform_set_drvdata(devptr, card);
  108. return 0;
  109. }
  110. __nodev:
  111. snd_card_free(card);
  112. return err;
  113. }
  114. static int __devexit snd_virmidi_remove(struct platform_device *devptr)
  115. {
  116. snd_card_free(platform_get_drvdata(devptr));
  117. platform_set_drvdata(devptr, NULL);
  118. return 0;
  119. }
  120. #define SND_VIRMIDI_DRIVER "snd_virmidi"
  121. static struct platform_driver snd_virmidi_driver = {
  122. .probe = snd_virmidi_probe,
  123. .remove = __devexit_p(snd_virmidi_remove),
  124. .driver = {
  125. .name = SND_VIRMIDI_DRIVER
  126. },
  127. };
  128. static void __init_or_module snd_virmidi_unregister_all(void)
  129. {
  130. int i;
  131. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  132. platform_device_unregister(devices[i]);
  133. platform_driver_unregister(&snd_virmidi_driver);
  134. }
  135. static int __init alsa_card_virmidi_init(void)
  136. {
  137. int i, cards, err;
  138. if ((err = platform_driver_register(&snd_virmidi_driver)) < 0)
  139. return err;
  140. cards = 0;
  141. for (i = 0; i < SNDRV_CARDS; i++) {
  142. struct platform_device *device;
  143. if (! enable[i])
  144. continue;
  145. device = platform_device_register_simple(SND_VIRMIDI_DRIVER,
  146. i, NULL, 0);
  147. if (IS_ERR(device))
  148. continue;
  149. if (!platform_get_drvdata(device)) {
  150. platform_device_unregister(device);
  151. continue;
  152. }
  153. devices[i] = device;
  154. cards++;
  155. }
  156. if (!cards) {
  157. #ifdef MODULE
  158. printk(KERN_ERR "Card-VirMIDI soundcard not found or device busy\n");
  159. #endif
  160. snd_virmidi_unregister_all();
  161. return -ENODEV;
  162. }
  163. return 0;
  164. }
  165. static void __exit alsa_card_virmidi_exit(void)
  166. {
  167. snd_virmidi_unregister_all();
  168. }
  169. module_init(alsa_card_virmidi_init)
  170. module_exit(alsa_card_virmidi_exit)