seq_dummy.c 5.8 KB

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