seq_dummy.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = 0;
  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. typedef struct snd_seq_dummy_port {
  61. int client;
  62. int port;
  63. int duplex;
  64. int connect;
  65. } snd_seq_dummy_port_t;
  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, snd_seq_port_subscribe_t *info)
  74. {
  75. snd_seq_dummy_port_t *p;
  76. int i;
  77. snd_seq_event_t 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(snd_seq_event_t *ev, int direct, void *private_data, int atomic, int hop)
  100. {
  101. snd_seq_dummy_port_t *p;
  102. snd_seq_event_t 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 snd_seq_dummy_port_t __init *
  127. create_port(int idx, int type)
  128. {
  129. snd_seq_port_info_t pinfo;
  130. snd_seq_port_callback_t pcb;
  131. snd_seq_dummy_port_t *rec;
  132. if ((rec = kcalloc(1, 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. memset(&pcb, 0, sizeof(pcb));
  150. pcb.owner = THIS_MODULE;
  151. pcb.unuse = dummy_unuse;
  152. pcb.event_input = dummy_input;
  153. pcb.private_free = dummy_free;
  154. pcb.private_data = rec;
  155. pinfo.kernel = &pcb;
  156. if (snd_seq_kernel_client_ctl(my_client, SNDRV_SEQ_IOCTL_CREATE_PORT, &pinfo) < 0) {
  157. kfree(rec);
  158. return NULL;
  159. }
  160. rec->port = pinfo.addr.port;
  161. return rec;
  162. }
  163. /*
  164. * register client and create ports
  165. */
  166. static int __init
  167. register_client(void)
  168. {
  169. snd_seq_client_callback_t cb;
  170. snd_seq_client_info_t cinfo;
  171. snd_seq_dummy_port_t *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. memset(&cb, 0, sizeof(cb));
  179. cb.allow_input = 1;
  180. cb.allow_output = 1;
  181. my_client = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_DUMMY, &cb);
  182. if (my_client < 0)
  183. return my_client;
  184. /* set client name */
  185. memset(&cinfo, 0, sizeof(cinfo));
  186. cinfo.client = my_client;
  187. cinfo.type = KERNEL_CLIENT;
  188. strcpy(cinfo.name, "Midi Through");
  189. snd_seq_kernel_client_ctl(my_client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
  190. /* create ports */
  191. for (i = 0; i < ports; i++) {
  192. rec1 = create_port(i, 0);
  193. if (rec1 == NULL) {
  194. snd_seq_delete_kernel_client(my_client);
  195. return -ENOMEM;
  196. }
  197. if (duplex) {
  198. rec2 = create_port(i, 1);
  199. if (rec2 == NULL) {
  200. snd_seq_delete_kernel_client(my_client);
  201. return -ENOMEM;
  202. }
  203. rec1->connect = rec2->port;
  204. rec2->connect = rec1->port;
  205. }
  206. }
  207. return 0;
  208. }
  209. /*
  210. * delete client if exists
  211. */
  212. static void __exit
  213. delete_client(void)
  214. {
  215. if (my_client >= 0)
  216. snd_seq_delete_kernel_client(my_client);
  217. }
  218. /*
  219. * Init part
  220. */
  221. static int __init alsa_seq_dummy_init(void)
  222. {
  223. int err;
  224. snd_seq_autoload_lock();
  225. err = register_client();
  226. snd_seq_autoload_unlock();
  227. return err;
  228. }
  229. static void __exit alsa_seq_dummy_exit(void)
  230. {
  231. delete_client();
  232. }
  233. module_init(alsa_seq_dummy_init)
  234. module_exit(alsa_seq_dummy_exit)