1 /*
2 * Generic MIDI synth driver for ALSA sequencer
3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@perex.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22 /*
23 Possible options for midisynth module:
24 - automatic opening of midi ports on first received event or subscription
25 (close will be performed when client leaves)
26 */
27
28
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/module.h>
34 #include <linux/mutex.h>
35 #include <sound/core.h>
36 #include <sound/rawmidi.h>
37 #include <sound/seq_kernel.h>
38 #include <sound/seq_device.h>
39 #include <sound/seq_midi_event.h>
40 #include <sound/initval.h>
41
42 MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@perex.cz>");
43 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
44 MODULE_LICENSE("GPL");
45 static int output_buffer_size = PAGE_SIZE;
46 module_param(output_buffer_size, int, 0644);
47 MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
48 static int input_buffer_size = PAGE_SIZE;
49 module_param(input_buffer_size, int, 0644);
50 MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
51
52 /* data for this midi synth driver */
53 struct seq_midisynth {
54 struct snd_card *card;
55 int device;
56 int subdevice;
57 struct snd_rawmidi_file input_rfile;
58 struct snd_rawmidi_file output_rfile;
59 int seq_client;
60 int seq_port;
61 struct snd_midi_event *parser;
62 };
63
64 struct seq_midisynth_client {
65 int seq_client;
66 int num_ports;
67 int ports_per_device[SNDRV_RAWMIDI_DEVICES];
68 struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES];
69 };
70
71 static struct seq_midisynth_client *synths[SNDRV_CARDS];
72 static DEFINE_MUTEX(register_mutex);
73
74 /* handle rawmidi input event (MIDI v1.0 stream) */
snd_midi_input_event(struct snd_rawmidi_substream * substream)75 static void snd_midi_input_event(struct snd_rawmidi_substream *substream)
76 {
77 struct snd_rawmidi_runtime *runtime;
78 struct seq_midisynth *msynth;
79 struct snd_seq_event ev;
80 char buf[16], *pbuf;
81 long res;
82
83 if (substream == NULL)
84 return;
85 runtime = substream->runtime;
86 msynth = runtime->private_data;
87 if (msynth == NULL)
88 return;
89 memset(&ev, 0, sizeof(ev));
90 while (runtime->avail > 0) {
91 res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
92 if (res <= 0)
93 continue;
94 if (msynth->parser == NULL)
95 continue;
96 pbuf = buf;
97 while (res-- > 0) {
98 if (!snd_midi_event_encode_byte(msynth->parser,
99 *pbuf++, &ev))
100 continue;
101 ev.source.port = msynth->seq_port;
102 ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
103 snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
104 /* clear event and reset header */
105 memset(&ev, 0, sizeof(ev));
106 }
107 }
108 }
109
dump_midi(struct snd_rawmidi_substream * substream,const char * buf,int count)110 static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count)
111 {
112 struct snd_rawmidi_runtime *runtime;
113 int tmp;
114
115 if (snd_BUG_ON(!substream || !buf))
116 return -EINVAL;
117 runtime = substream->runtime;
118 if ((tmp = runtime->avail) < count) {
119 if (printk_ratelimit())
120 pr_err("ALSA: seq_midi: MIDI output buffer overrun\n");
121 return -ENOMEM;
122 }
123 if (snd_rawmidi_kernel_write(substream, buf, count) < count)
124 return -EINVAL;
125 return 0;
126 }
127
event_process_midi(struct snd_seq_event * ev,int direct,void * private_data,int atomic,int hop)128 static int event_process_midi(struct snd_seq_event *ev, int direct,
129 void *private_data, int atomic, int hop)
130 {
131 struct seq_midisynth *msynth = private_data;
132 unsigned char msg[10]; /* buffer for constructing midi messages */
133 struct snd_rawmidi_substream *substream;
134 int len;
135
136 if (snd_BUG_ON(!msynth))
137 return -EINVAL;
138 substream = msynth->output_rfile.output;
139 if (substream == NULL)
140 return -ENODEV;
141 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */
142 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
143 /* invalid event */
144 pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
145 return 0;
146 }
147 snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
148 snd_midi_event_reset_decode(msynth->parser);
149 } else {
150 if (msynth->parser == NULL)
151 return -EIO;
152 len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
153 if (len < 0)
154 return 0;
155 if (dump_midi(substream, msg, len) < 0)
156 snd_midi_event_reset_decode(msynth->parser);
157 }
158 return 0;
159 }
160
161
snd_seq_midisynth_new(struct seq_midisynth * msynth,struct snd_card * card,int device,int subdevice)162 static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
163 struct snd_card *card,
164 int device,
165 int subdevice)
166 {
167 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
168 return -ENOMEM;
169 msynth->card = card;
170 msynth->device = device;
171 msynth->subdevice = subdevice;
172 return 0;
173 }
174
175 /* open associated midi device for input */
midisynth_subscribe(void * private_data,struct snd_seq_port_subscribe * info)176 static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
177 {
178 int err;
179 struct seq_midisynth *msynth = private_data;
180 struct snd_rawmidi_runtime *runtime;
181 struct snd_rawmidi_params params;
182
183 /* open midi port */
184 if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
185 msynth->subdevice,
186 SNDRV_RAWMIDI_LFLG_INPUT,
187 &msynth->input_rfile)) < 0) {
188 pr_debug("ALSA: seq_midi: midi input open failed!!!\n");
189 return err;
190 }
191 runtime = msynth->input_rfile.input->runtime;
192 memset(¶ms, 0, sizeof(params));
193 params.avail_min = 1;
194 params.buffer_size = input_buffer_size;
195 if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, ¶ms)) < 0) {
196 snd_rawmidi_kernel_release(&msynth->input_rfile);
197 return err;
198 }
199 snd_midi_event_reset_encode(msynth->parser);
200 runtime->event = snd_midi_input_event;
201 runtime->private_data = msynth;
202 snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
203 return 0;
204 }
205
206 /* close associated midi device for input */
midisynth_unsubscribe(void * private_data,struct snd_seq_port_subscribe * info)207 static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
208 {
209 int err;
210 struct seq_midisynth *msynth = private_data;
211
212 if (snd_BUG_ON(!msynth->input_rfile.input))
213 return -EINVAL;
214 err = snd_rawmidi_kernel_release(&msynth->input_rfile);
215 return err;
216 }
217
218 /* open associated midi device for output */
midisynth_use(void * private_data,struct snd_seq_port_subscribe * info)219 static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
220 {
221 int err;
222 struct seq_midisynth *msynth = private_data;
223 struct snd_rawmidi_params params;
224
225 /* open midi port */
226 if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
227 msynth->subdevice,
228 SNDRV_RAWMIDI_LFLG_OUTPUT,
229 &msynth->output_rfile)) < 0) {
230 pr_debug("ALSA: seq_midi: midi output open failed!!!\n");
231 return err;
232 }
233 memset(¶ms, 0, sizeof(params));
234 params.avail_min = 1;
235 params.buffer_size = output_buffer_size;
236 params.no_active_sensing = 1;
237 if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, ¶ms)) < 0) {
238 snd_rawmidi_kernel_release(&msynth->output_rfile);
239 return err;
240 }
241 snd_midi_event_reset_decode(msynth->parser);
242 return 0;
243 }
244
245 /* close associated midi device for output */
midisynth_unuse(void * private_data,struct snd_seq_port_subscribe * info)246 static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
247 {
248 struct seq_midisynth *msynth = private_data;
249
250 if (snd_BUG_ON(!msynth->output_rfile.output))
251 return -EINVAL;
252 snd_rawmidi_drain_output(msynth->output_rfile.output);
253 return snd_rawmidi_kernel_release(&msynth->output_rfile);
254 }
255
256 /* delete given midi synth port */
snd_seq_midisynth_delete(struct seq_midisynth * msynth)257 static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
258 {
259 if (msynth == NULL)
260 return;
261
262 if (msynth->seq_client > 0) {
263 /* delete port */
264 snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
265 }
266
267 snd_midi_event_free(msynth->parser);
268 }
269
270 /* register new midi synth port */
271 static int
snd_seq_midisynth_probe(struct device * _dev)272 snd_seq_midisynth_probe(struct device *_dev)
273 {
274 struct snd_seq_device *dev = to_seq_dev(_dev);
275 struct seq_midisynth_client *client;
276 struct seq_midisynth *msynth, *ms;
277 struct snd_seq_port_info *port;
278 struct snd_rawmidi_info *info;
279 struct snd_rawmidi *rmidi = dev->private_data;
280 int newclient = 0;
281 unsigned int p, ports;
282 struct snd_seq_port_callback pcallbacks;
283 struct snd_card *card = dev->card;
284 int device = dev->device;
285 unsigned int input_count = 0, output_count = 0;
286
287 if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
288 return -EINVAL;
289 info = kmalloc(sizeof(*info), GFP_KERNEL);
290 if (! info)
291 return -ENOMEM;
292 info->device = device;
293 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
294 info->subdevice = 0;
295 if (snd_rawmidi_info_select(card, info) >= 0)
296 output_count = info->subdevices_count;
297 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
298 if (snd_rawmidi_info_select(card, info) >= 0) {
299 input_count = info->subdevices_count;
300 }
301 ports = output_count;
302 if (ports < input_count)
303 ports = input_count;
304 if (ports == 0) {
305 kfree(info);
306 return -ENODEV;
307 }
308 if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
309 ports = 256 / SNDRV_RAWMIDI_DEVICES;
310
311 mutex_lock(®ister_mutex);
312 client = synths[card->number];
313 if (client == NULL) {
314 newclient = 1;
315 client = kzalloc(sizeof(*client), GFP_KERNEL);
316 if (client == NULL) {
317 mutex_unlock(®ister_mutex);
318 kfree(info);
319 return -ENOMEM;
320 }
321 client->seq_client =
322 snd_seq_create_kernel_client(
323 card, 0, "%s", card->shortname[0] ?
324 (const char *)card->shortname : "External MIDI");
325 if (client->seq_client < 0) {
326 kfree(client);
327 mutex_unlock(®ister_mutex);
328 kfree(info);
329 return -ENOMEM;
330 }
331 }
332
333 msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
334 port = kmalloc(sizeof(*port), GFP_KERNEL);
335 if (msynth == NULL || port == NULL)
336 goto __nomem;
337
338 for (p = 0; p < ports; p++) {
339 ms = &msynth[p];
340
341 if (snd_seq_midisynth_new(ms, card, device, p) < 0)
342 goto __nomem;
343
344 /* declare port */
345 memset(port, 0, sizeof(*port));
346 port->addr.client = client->seq_client;
347 port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
348 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
349 memset(info, 0, sizeof(*info));
350 info->device = device;
351 if (p < output_count)
352 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
353 else
354 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
355 info->subdevice = p;
356 if (snd_rawmidi_info_select(card, info) >= 0)
357 strcpy(port->name, info->subname);
358 if (! port->name[0]) {
359 if (info->name[0]) {
360 if (ports > 1)
361 snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
362 else
363 snprintf(port->name, sizeof(port->name), "%s", info->name);
364 } else {
365 /* last resort */
366 if (ports > 1)
367 sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
368 else
369 sprintf(port->name, "MIDI %d-%d", card->number, device);
370 }
371 }
372 if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
373 port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
374 if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
375 port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
376 if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
377 info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
378 port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
379 port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
380 | SNDRV_SEQ_PORT_TYPE_HARDWARE
381 | SNDRV_SEQ_PORT_TYPE_PORT;
382 port->midi_channels = 16;
383 memset(&pcallbacks, 0, sizeof(pcallbacks));
384 pcallbacks.owner = THIS_MODULE;
385 pcallbacks.private_data = ms;
386 pcallbacks.subscribe = midisynth_subscribe;
387 pcallbacks.unsubscribe = midisynth_unsubscribe;
388 pcallbacks.use = midisynth_use;
389 pcallbacks.unuse = midisynth_unuse;
390 pcallbacks.event_input = event_process_midi;
391 port->kernel = &pcallbacks;
392 if (rmidi->ops && rmidi->ops->get_port_info)
393 rmidi->ops->get_port_info(rmidi, p, port);
394 if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
395 goto __nomem;
396 ms->seq_client = client->seq_client;
397 ms->seq_port = port->addr.port;
398 }
399 client->ports_per_device[device] = ports;
400 client->ports[device] = msynth;
401 client->num_ports++;
402 if (newclient)
403 synths[card->number] = client;
404 mutex_unlock(®ister_mutex);
405 kfree(info);
406 kfree(port);
407 return 0; /* success */
408
409 __nomem:
410 if (msynth != NULL) {
411 for (p = 0; p < ports; p++)
412 snd_seq_midisynth_delete(&msynth[p]);
413 kfree(msynth);
414 }
415 if (newclient) {
416 snd_seq_delete_kernel_client(client->seq_client);
417 kfree(client);
418 }
419 kfree(info);
420 kfree(port);
421 mutex_unlock(®ister_mutex);
422 return -ENOMEM;
423 }
424
425 /* release midi synth port */
426 static int
snd_seq_midisynth_remove(struct device * _dev)427 snd_seq_midisynth_remove(struct device *_dev)
428 {
429 struct snd_seq_device *dev = to_seq_dev(_dev);
430 struct seq_midisynth_client *client;
431 struct seq_midisynth *msynth;
432 struct snd_card *card = dev->card;
433 int device = dev->device, p, ports;
434
435 mutex_lock(®ister_mutex);
436 client = synths[card->number];
437 if (client == NULL || client->ports[device] == NULL) {
438 mutex_unlock(®ister_mutex);
439 return -ENODEV;
440 }
441 ports = client->ports_per_device[device];
442 client->ports_per_device[device] = 0;
443 msynth = client->ports[device];
444 client->ports[device] = NULL;
445 for (p = 0; p < ports; p++)
446 snd_seq_midisynth_delete(&msynth[p]);
447 kfree(msynth);
448 client->num_ports--;
449 if (client->num_ports <= 0) {
450 snd_seq_delete_kernel_client(client->seq_client);
451 synths[card->number] = NULL;
452 kfree(client);
453 }
454 mutex_unlock(®ister_mutex);
455 return 0;
456 }
457
458 static struct snd_seq_driver seq_midisynth_driver = {
459 .driver = {
460 .name = KBUILD_MODNAME,
461 .probe = snd_seq_midisynth_probe,
462 .remove = snd_seq_midisynth_remove,
463 },
464 .id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
465 .argsize = 0,
466 };
467
468 module_snd_seq_driver(seq_midisynth_driver);
469