Linux Audio

Check our new training course

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