seq_dummy.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/moduleparam.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 /etc/modprobe.conf:
  42. option snd-seq-dummy ports=4
  43. The modle 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 int 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. * unuse callback - send ALL_SOUNDS_OFF and RESET_CONTROLLERS events
  68. * to subscribers.
  69. * Note: this callback is called only after all subscribers are removed.
  70. */
  71. static int
  72. dummy_unuse(void *private_data, struct snd_seq_port_subscribe *info)
  73. {
  74. struct snd_seq_dummy_port *p;
  75. int i;
  76. struct snd_seq_event ev;
  77. p = private_data;
  78. memset(&ev, 0, sizeof(ev));
  79. if (p->duplex)
  80. ev.source.port = p->connect;
  81. else
  82. ev.source.port = p->port;
  83. ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  84. ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
  85. for (i = 0; i < 16; i++) {
  86. ev.data.control.channel = i;
  87. ev.data.control.param = MIDI_CTL_ALL_SOUNDS_OFF;
  88. snd_seq_kernel_client_dispatch(p->client, &ev, 0, 0);
  89. ev.data.control.param = MIDI_CTL_RESET_CONTROLLERS;
  90. snd_seq_kernel_client_dispatch(p->client, &ev, 0, 0);
  91. }
  92. return 0;
  93. }
  94. /*
  95. * event input callback - just redirect events to subscribers
  96. */
  97. static int
  98. dummy_input(struct snd_seq_event *ev, int direct, void *private_data,
  99. int atomic, int hop)
  100. {
  101. struct snd_seq_dummy_port *p;
  102. struct snd_seq_event tmpev;
  103. p = private_data;
  104. if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM ||
  105. ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
  106. return 0; /* ignore system messages */
  107. tmpev = *ev;
  108. if (p->duplex)
  109. tmpev.source.port = p->connect;
  110. else
  111. tmpev.source.port = p->port;
  112. tmpev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  113. return snd_seq_kernel_client_dispatch(p->client, &tmpev, atomic, hop);
  114. }
  115. /*
  116. * free_private callback
  117. */
  118. static void
  119. dummy_free(void *private_data)
  120. {
  121. kfree(private_data);
  122. }
  123. /*
  124. * create a port
  125. */
  126. static struct snd_seq_dummy_port __init *
  127. create_port(int idx, int type)
  128. {
  129. struct snd_seq_port_info pinfo;
  130. struct snd_seq_port_callback pcb;
  131. struct snd_seq_dummy_port *rec;
  132. if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL)
  133. return NULL;
  134. rec->client = my_client;
  135. rec->duplex = duplex;
  136. rec->connect = 0;
  137. memset(&pinfo, 0, sizeof(pinfo));
  138. pinfo.addr.client = my_client;
  139. if (duplex)
  140. sprintf(pinfo.name, "Midi Through Port-%d:%c", idx,
  141. (type ? 'B' : 'A'));
  142. else
  143. sprintf(pinfo.name, "Midi Through Port-%d", idx);
  144. pinfo.capability = SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  145. pinfo.capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  146. if (duplex)
  147. pinfo.capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  148. pinfo.type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  149. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  150. | SNDRV_SEQ_PORT_TYPE_PORT;
  151. memset(&pcb, 0, sizeof(pcb));
  152. pcb.owner = THIS_MODULE;
  153. pcb.unuse = dummy_unuse;
  154. pcb.event_input = dummy_input;
  155. pcb.private_free = dummy_free;
  156. pcb.private_data = rec;
  157. pinfo.kernel = &pcb;
  158. if (snd_seq_kernel_client_ctl(my_client, SNDRV_SEQ_IOCTL_CREATE_PORT, &pinfo) < 0) {
  159. kfree(rec);
  160. return NULL;
  161. }
  162. rec->port = pinfo.addr.port;
  163. return rec;
  164. }
  165. /*
  166. * register client and create ports
  167. */
  168. static int __init
  169. register_client(void)
  170. {
  171. struct snd_seq_dummy_port *rec1, *rec2;
  172. int i;
  173. if (ports < 1) {
  174. snd_printk(KERN_ERR "invalid number of ports %d\n", ports);
  175. return -EINVAL;
  176. }
  177. /* create client */
  178. my_client = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_DUMMY,
  179. "Midi Through");
  180. if (my_client < 0)
  181. return my_client;
  182. /* create ports */
  183. for (i = 0; i < ports; i++) {
  184. rec1 = create_port(i, 0);
  185. if (rec1 == NULL) {
  186. snd_seq_delete_kernel_client(my_client);
  187. return -ENOMEM;
  188. }
  189. if (duplex) {
  190. rec2 = create_port(i, 1);
  191. if (rec2 == NULL) {
  192. snd_seq_delete_kernel_client(my_client);
  193. return -ENOMEM;
  194. }
  195. rec1->connect = rec2->port;
  196. rec2->connect = rec1->port;
  197. }
  198. }
  199. return 0;
  200. }
  201. /*
  202. * delete client if exists
  203. */
  204. static void __exit
  205. delete_client(void)
  206. {
  207. if (my_client >= 0)
  208. snd_seq_delete_kernel_client(my_client);
  209. }
  210. /*
  211. * Init part
  212. */
  213. static int __init alsa_seq_dummy_init(void)
  214. {
  215. int err;
  216. snd_seq_autoload_lock();
  217. err = register_client();
  218. snd_seq_autoload_unlock();
  219. return err;
  220. }
  221. static void __exit alsa_seq_dummy_exit(void)
  222. {
  223. delete_client();
  224. }
  225. module_init(alsa_seq_dummy_init)
  226. module_exit(alsa_seq_dummy_exit)