seq_dummy.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/moduleparam.h>
  24. #include <sound/core.h>
  25. #include "seq_clientmgr.h"
  26. #include <sound/initval.h>
  27. #include <sound/asoundef.h>
  28. /*
  29. Sequencer MIDI-through client
  30. This gives a simple midi-through client. All the normal input events
  31. are redirected to output port immediately.
  32. The routing can be done via aconnect program in alsa-utils.
  33. Each client has a static client number 62 (= SNDRV_SEQ_CLIENT_DUMMY).
  34. If you want to auto-load this module, you may add the following alias
  35. in your /etc/conf.modules file.
  36. alias snd-seq-client-62 snd-seq-dummy
  37. The module is loaded on demand for client 62, or /proc/asound/seq/
  38. is accessed. If you don't need this module to be loaded, alias
  39. snd-seq-client-62 as "off". This will help modprobe.
  40. The number of ports to be created can be specified via the module
  41. parameter "ports". For example, to create four ports, add the
  42. following option in /etc/modprobe.conf:
  43. option snd-seq-dummy ports=4
  44. The modle option "duplex=1" enables duplex operation to the port.
  45. In duplex mode, a pair of ports are created instead of single port,
  46. and events are tunneled between pair-ports. For example, input to
  47. port A is sent to output port of another port B and vice versa.
  48. In duplex mode, each port has DUPLEX capability.
  49. */
  50. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  51. MODULE_DESCRIPTION("ALSA sequencer MIDI-through client");
  52. MODULE_LICENSE("GPL");
  53. MODULE_ALIAS("snd-seq-client-" __stringify(SNDRV_SEQ_CLIENT_DUMMY));
  54. static int ports = 1;
  55. static int duplex;
  56. module_param(ports, int, 0444);
  57. MODULE_PARM_DESC(ports, "number of ports to be created");
  58. module_param(duplex, bool, 0444);
  59. MODULE_PARM_DESC(duplex, "create DUPLEX ports");
  60. struct snd_seq_dummy_port {
  61. int client;
  62. int port;
  63. int duplex;
  64. int connect;
  65. };
  66. static int my_client = -1;
  67. /*
  68. * unuse callback - send ALL_SOUNDS_OFF and RESET_CONTROLLERS events
  69. * to subscribers.
  70. * Note: this callback is called only after all subscribers are removed.
  71. */
  72. static int
  73. dummy_unuse(void *private_data, struct snd_seq_port_subscribe *info)
  74. {
  75. struct snd_seq_dummy_port *p;
  76. int i;
  77. struct snd_seq_event ev;
  78. p = private_data;
  79. memset(&ev, 0, sizeof(ev));
  80. if (p->duplex)
  81. ev.source.port = p->connect;
  82. else
  83. ev.source.port = p->port;
  84. ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  85. ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
  86. for (i = 0; i < 16; i++) {
  87. ev.data.control.channel = i;
  88. ev.data.control.param = MIDI_CTL_ALL_SOUNDS_OFF;
  89. snd_seq_kernel_client_dispatch(p->client, &ev, 0, 0);
  90. ev.data.control.param = MIDI_CTL_RESET_CONTROLLERS;
  91. snd_seq_kernel_client_dispatch(p->client, &ev, 0, 0);
  92. }
  93. return 0;
  94. }
  95. /*
  96. * event input callback - just redirect events to subscribers
  97. */
  98. static int
  99. dummy_input(struct snd_seq_event *ev, int direct, void *private_data,
  100. int atomic, int hop)
  101. {
  102. struct snd_seq_dummy_port *p;
  103. struct snd_seq_event tmpev;
  104. p = private_data;
  105. if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM ||
  106. ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
  107. return 0; /* ignore system messages */
  108. tmpev = *ev;
  109. if (p->duplex)
  110. tmpev.source.port = p->connect;
  111. else
  112. tmpev.source.port = p->port;
  113. tmpev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  114. return snd_seq_kernel_client_dispatch(p->client, &tmpev, atomic, hop);
  115. }
  116. /*
  117. * free_private callback
  118. */
  119. static void
  120. dummy_free(void *private_data)
  121. {
  122. kfree(private_data);
  123. }
  124. /*
  125. * create a port
  126. */
  127. static struct snd_seq_dummy_port __init *
  128. create_port(int idx, int type)
  129. {
  130. struct snd_seq_port_info pinfo;
  131. struct snd_seq_port_callback pcb;
  132. struct snd_seq_dummy_port *rec;
  133. if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL)
  134. return NULL;
  135. rec->client = my_client;
  136. rec->duplex = duplex;
  137. rec->connect = 0;
  138. memset(&pinfo, 0, sizeof(pinfo));
  139. pinfo.addr.client = my_client;
  140. if (duplex)
  141. sprintf(pinfo.name, "Midi Through Port-%d:%c", idx,
  142. (type ? 'B' : 'A'));
  143. else
  144. sprintf(pinfo.name, "Midi Through Port-%d", idx);
  145. pinfo.capability = SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  146. pinfo.capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  147. if (duplex)
  148. pinfo.capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  149. pinfo.type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  150. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  151. | SNDRV_SEQ_PORT_TYPE_PORT;
  152. memset(&pcb, 0, sizeof(pcb));
  153. pcb.owner = THIS_MODULE;
  154. pcb.unuse = dummy_unuse;
  155. pcb.event_input = dummy_input;
  156. pcb.private_free = dummy_free;
  157. pcb.private_data = rec;
  158. pinfo.kernel = &pcb;
  159. if (snd_seq_kernel_client_ctl(my_client, SNDRV_SEQ_IOCTL_CREATE_PORT, &pinfo) < 0) {
  160. kfree(rec);
  161. return NULL;
  162. }
  163. rec->port = pinfo.addr.port;
  164. return rec;
  165. }
  166. /*
  167. * register client and create ports
  168. */
  169. static int __init
  170. register_client(void)
  171. {
  172. struct snd_seq_dummy_port *rec1, *rec2;
  173. int i;
  174. if (ports < 1) {
  175. snd_printk(KERN_ERR "invalid number of ports %d\n", ports);
  176. return -EINVAL;
  177. }
  178. /* create client */
  179. my_client = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_DUMMY,
  180. "Midi Through");
  181. if (my_client < 0)
  182. return my_client;
  183. /* create ports */
  184. for (i = 0; i < ports; i++) {
  185. rec1 = create_port(i, 0);
  186. if (rec1 == NULL) {
  187. snd_seq_delete_kernel_client(my_client);
  188. return -ENOMEM;
  189. }
  190. if (duplex) {
  191. rec2 = create_port(i, 1);
  192. if (rec2 == NULL) {
  193. snd_seq_delete_kernel_client(my_client);
  194. return -ENOMEM;
  195. }
  196. rec1->connect = rec2->port;
  197. rec2->connect = rec1->port;
  198. }
  199. }
  200. return 0;
  201. }
  202. /*
  203. * delete client if exists
  204. */
  205. static void __exit
  206. delete_client(void)
  207. {
  208. if (my_client >= 0)
  209. snd_seq_delete_kernel_client(my_client);
  210. }
  211. /*
  212. * Init part
  213. */
  214. static int __init alsa_seq_dummy_init(void)
  215. {
  216. int err;
  217. snd_seq_autoload_lock();
  218. err = register_client();
  219. snd_seq_autoload_unlock();
  220. return err;
  221. }
  222. static void __exit alsa_seq_dummy_exit(void)
  223. {
  224. delete_client();
  225. }
  226. module_init(alsa_seq_dummy_init)
  227. module_exit(alsa_seq_dummy_exit)