seq_dummy.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * ALSA sequencer MIDI-through client
  3. * Copyright (c) 1999-2000 by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <sound/core.h>
  24. #include "seq_clientmgr.h"
  25. #include <sound/initval.h>
  26. #include <sound/asoundef.h>
  27. /*
  28. Sequencer MIDI-through client
  29. This gives a simple midi-through client. All the normal input events
  30. are redirected to output port immediately.
  31. The routing can be done via aconnect program in alsa-utils.
  32. Each client has a static client number 62 (= SNDRV_SEQ_CLIENT_DUMMY).
  33. If you want to auto-load this module, you may add the following alias
  34. in your /etc/conf.modules file.
  35. alias snd-seq-client-62 snd-seq-dummy
  36. The module is loaded on demand for client 62, or /proc/asound/seq/
  37. is accessed. If you don't need this module to be loaded, alias
  38. snd-seq-client-62 as "off". This will help modprobe.
  39. The number of ports to be created can be specified via the module
  40. parameter "ports". For example, to create four ports, add the
  41. following option in a configuration file under /etc/modprobe.d/:
  42. option snd-seq-dummy ports=4
  43. The model option "duplex=1" enables duplex operation to the port.
  44. In duplex mode, a pair of ports are created instead of single port,
  45. and events are tunneled between pair-ports. For example, input to
  46. port A is sent to output port of another port B and vice versa.
  47. In duplex mode, each port has DUPLEX capability.
  48. */
  49. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  50. MODULE_DESCRIPTION("ALSA sequencer MIDI-through client");
  51. MODULE_LICENSE("GPL");
  52. MODULE_ALIAS("snd-seq-client-" __stringify(SNDRV_SEQ_CLIENT_DUMMY));
  53. static int ports = 1;
  54. static bool duplex;
  55. module_param(ports, int, 0444);
  56. MODULE_PARM_DESC(ports, "number of ports to be created");
  57. module_param(duplex, bool, 0444);
  58. MODULE_PARM_DESC(duplex, "create DUPLEX ports");
  59. struct snd_seq_dummy_port {
  60. int client;
  61. int port;
  62. int duplex;
  63. int connect;
  64. };
  65. static int my_client = -1;
  66. /*
  67. * event input callback - just redirect events to subscribers
  68. */
  69. static int
  70. dummy_input(struct snd_seq_event *ev, int direct, void *private_data,
  71. int atomic, int hop)
  72. {
  73. struct snd_seq_dummy_port *p;
  74. struct snd_seq_event tmpev;
  75. p = private_data;
  76. if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM ||
  77. ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
  78. return 0; /* ignore system messages */
  79. tmpev = *ev;
  80. if (p->duplex)
  81. tmpev.source.port = p->connect;
  82. else
  83. tmpev.source.port = p->port;
  84. tmpev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  85. return snd_seq_kernel_client_dispatch(p->client, &tmpev, atomic, hop);
  86. }
  87. /*
  88. * free_private callback
  89. */
  90. static void
  91. dummy_free(void *private_data)
  92. {
  93. kfree(private_data);
  94. }
  95. /*
  96. * create a port
  97. */
  98. static struct snd_seq_dummy_port __init *
  99. create_port(int idx, int type)
  100. {
  101. struct snd_seq_port_info pinfo;
  102. struct snd_seq_port_callback pcb;
  103. struct snd_seq_dummy_port *rec;
  104. if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL)
  105. return NULL;
  106. rec->client = my_client;
  107. rec->duplex = duplex;
  108. rec->connect = 0;
  109. memset(&pinfo, 0, sizeof(pinfo));
  110. pinfo.addr.client = my_client;
  111. if (duplex)
  112. sprintf(pinfo.name, "Midi Through Port-%d:%c", idx,
  113. (type ? 'B' : 'A'));
  114. else
  115. sprintf(pinfo.name, "Midi Through Port-%d", idx);
  116. pinfo.capability = SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  117. pinfo.capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  118. if (duplex)
  119. pinfo.capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  120. pinfo.type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  121. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  122. | SNDRV_SEQ_PORT_TYPE_PORT;
  123. memset(&pcb, 0, sizeof(pcb));
  124. pcb.owner = THIS_MODULE;
  125. pcb.event_input = dummy_input;
  126. pcb.private_free = dummy_free;
  127. pcb.private_data = rec;
  128. pinfo.kernel = &pcb;
  129. if (snd_seq_kernel_client_ctl(my_client, SNDRV_SEQ_IOCTL_CREATE_PORT, &pinfo) < 0) {
  130. kfree(rec);
  131. return NULL;
  132. }
  133. rec->port = pinfo.addr.port;
  134. return rec;
  135. }
  136. /*
  137. * register client and create ports
  138. */
  139. static int __init
  140. register_client(void)
  141. {
  142. struct snd_seq_dummy_port *rec1, *rec2;
  143. int i;
  144. if (ports < 1) {
  145. pr_err("ALSA: seq_dummy: invalid number of ports %d\n", ports);
  146. return -EINVAL;
  147. }
  148. /* create client */
  149. my_client = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_DUMMY,
  150. "Midi Through");
  151. if (my_client < 0)
  152. return my_client;
  153. /* create ports */
  154. for (i = 0; i < ports; i++) {
  155. rec1 = create_port(i, 0);
  156. if (rec1 == NULL) {
  157. snd_seq_delete_kernel_client(my_client);
  158. return -ENOMEM;
  159. }
  160. if (duplex) {
  161. rec2 = create_port(i, 1);
  162. if (rec2 == NULL) {
  163. snd_seq_delete_kernel_client(my_client);
  164. return -ENOMEM;
  165. }
  166. rec1->connect = rec2->port;
  167. rec2->connect = rec1->port;
  168. }
  169. }
  170. return 0;
  171. }
  172. /*
  173. * delete client if exists
  174. */
  175. static void __exit
  176. delete_client(void)
  177. {
  178. if (my_client >= 0)
  179. snd_seq_delete_kernel_client(my_client);
  180. }
  181. /*
  182. * Init part
  183. */
  184. static int __init alsa_seq_dummy_init(void)
  185. {
  186. return register_client();
  187. }
  188. static void __exit alsa_seq_dummy_exit(void)
  189. {
  190. delete_client();
  191. }
  192. module_init(alsa_seq_dummy_init)
  193. module_exit(alsa_seq_dummy_exit)