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