1 /*
2  * OSS compatible sequencer driver
3  *
4  * MIDI device handlers
5  *
6  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 
23 #include <sound/asoundef.h>
24 #include "seq_oss_midi.h"
25 #include "seq_oss_readq.h"
26 #include "seq_oss_timer.h"
27 #include "seq_oss_event.h"
28 #include <sound/seq_midi_event.h>
29 #include "../seq_lock.h"
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 
33 
34 /*
35  * constants
36  */
37 #define SNDRV_SEQ_OSS_MAX_MIDI_NAME	30
38 
39 /*
40  * definition of midi device record
41  */
42 struct seq_oss_midi {
43 	int seq_device;		/* device number */
44 	int client;		/* sequencer client number */
45 	int port;		/* sequencer port number */
46 	unsigned int flags;	/* port capability */
47 	int opened;		/* flag for opening */
48 	unsigned char name[SNDRV_SEQ_OSS_MAX_MIDI_NAME];
49 	struct snd_midi_event *coder;	/* MIDI event coder */
50 	struct seq_oss_devinfo *devinfo;	/* assigned OSSseq device */
51 	snd_use_lock_t use_lock;
52 };
53 
54 
55 /*
56  * midi device table
57  */
58 static int max_midi_devs;
59 static struct seq_oss_midi *midi_devs[SNDRV_SEQ_OSS_MAX_MIDI_DEVS];
60 
61 static DEFINE_SPINLOCK(register_lock);
62 
63 /*
64  * prototypes
65  */
66 static struct seq_oss_midi *get_mdev(int dev);
67 static struct seq_oss_midi *get_mididev(struct seq_oss_devinfo *dp, int dev);
68 static int send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev);
69 static int send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev);
70 
71 /*
72  * look up the existing ports
73  * this looks a very exhausting job.
74  */
75 int
snd_seq_oss_midi_lookup_ports(int client)76 snd_seq_oss_midi_lookup_ports(int client)
77 {
78 	struct snd_seq_client_info *clinfo;
79 	struct snd_seq_port_info *pinfo;
80 
81 	clinfo = kzalloc(sizeof(*clinfo), GFP_KERNEL);
82 	pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
83 	if (! clinfo || ! pinfo) {
84 		kfree(clinfo);
85 		kfree(pinfo);
86 		return -ENOMEM;
87 	}
88 	clinfo->client = -1;
89 	while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, clinfo) == 0) {
90 		if (clinfo->client == client)
91 			continue; /* ignore myself */
92 		pinfo->addr.client = clinfo->client;
93 		pinfo->addr.port = -1;
94 		while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, pinfo) == 0)
95 			snd_seq_oss_midi_check_new_port(pinfo);
96 	}
97 	kfree(clinfo);
98 	kfree(pinfo);
99 	return 0;
100 }
101 
102 
103 /*
104  */
105 static struct seq_oss_midi *
get_mdev(int dev)106 get_mdev(int dev)
107 {
108 	struct seq_oss_midi *mdev;
109 	unsigned long flags;
110 
111 	spin_lock_irqsave(&register_lock, flags);
112 	mdev = midi_devs[dev];
113 	if (mdev)
114 		snd_use_lock_use(&mdev->use_lock);
115 	spin_unlock_irqrestore(&register_lock, flags);
116 	return mdev;
117 }
118 
119 /*
120  * look for the identical slot
121  */
122 static struct seq_oss_midi *
find_slot(int client,int port)123 find_slot(int client, int port)
124 {
125 	int i;
126 	struct seq_oss_midi *mdev;
127 	unsigned long flags;
128 
129 	spin_lock_irqsave(&register_lock, flags);
130 	for (i = 0; i < max_midi_devs; i++) {
131 		mdev = midi_devs[i];
132 		if (mdev && mdev->client == client && mdev->port == port) {
133 			/* found! */
134 			snd_use_lock_use(&mdev->use_lock);
135 			spin_unlock_irqrestore(&register_lock, flags);
136 			return mdev;
137 		}
138 	}
139 	spin_unlock_irqrestore(&register_lock, flags);
140 	return NULL;
141 }
142 
143 
144 #define PERM_WRITE (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
145 #define PERM_READ (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
146 /*
147  * register a new port if it doesn't exist yet
148  */
149 int
snd_seq_oss_midi_check_new_port(struct snd_seq_port_info * pinfo)150 snd_seq_oss_midi_check_new_port(struct snd_seq_port_info *pinfo)
151 {
152 	int i;
153 	struct seq_oss_midi *mdev;
154 	unsigned long flags;
155 
156 	/* the port must include generic midi */
157 	if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
158 		return 0;
159 	/* either read or write subscribable */
160 	if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
161 	    (pinfo->capability & PERM_READ) != PERM_READ)
162 		return 0;
163 
164 	/*
165 	 * look for the identical slot
166 	 */
167 	if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
168 		/* already exists */
169 		snd_use_lock_free(&mdev->use_lock);
170 		return 0;
171 	}
172 
173 	/*
174 	 * allocate midi info record
175 	 */
176 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
177 	if (!mdev)
178 		return -ENOMEM;
179 
180 	/* copy the port information */
181 	mdev->client = pinfo->addr.client;
182 	mdev->port = pinfo->addr.port;
183 	mdev->flags = pinfo->capability;
184 	mdev->opened = 0;
185 	snd_use_lock_init(&mdev->use_lock);
186 
187 	/* copy and truncate the name of synth device */
188 	strlcpy(mdev->name, pinfo->name, sizeof(mdev->name));
189 
190 	/* create MIDI coder */
191 	if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
192 		pr_err("ALSA: seq_oss: can't malloc midi coder\n");
193 		kfree(mdev);
194 		return -ENOMEM;
195 	}
196 	/* OSS sequencer adds running status to all sequences */
197 	snd_midi_event_no_status(mdev->coder, 1);
198 
199 	/*
200 	 * look for en empty slot
201 	 */
202 	spin_lock_irqsave(&register_lock, flags);
203 	for (i = 0; i < max_midi_devs; i++) {
204 		if (midi_devs[i] == NULL)
205 			break;
206 	}
207 	if (i >= max_midi_devs) {
208 		if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
209 			spin_unlock_irqrestore(&register_lock, flags);
210 			snd_midi_event_free(mdev->coder);
211 			kfree(mdev);
212 			return -ENOMEM;
213 		}
214 		max_midi_devs++;
215 	}
216 	mdev->seq_device = i;
217 	midi_devs[mdev->seq_device] = mdev;
218 	spin_unlock_irqrestore(&register_lock, flags);
219 
220 	return 0;
221 }
222 
223 /*
224  * release the midi device if it was registered
225  */
226 int
snd_seq_oss_midi_check_exit_port(int client,int port)227 snd_seq_oss_midi_check_exit_port(int client, int port)
228 {
229 	struct seq_oss_midi *mdev;
230 	unsigned long flags;
231 	int index;
232 
233 	if ((mdev = find_slot(client, port)) != NULL) {
234 		spin_lock_irqsave(&register_lock, flags);
235 		midi_devs[mdev->seq_device] = NULL;
236 		spin_unlock_irqrestore(&register_lock, flags);
237 		snd_use_lock_free(&mdev->use_lock);
238 		snd_use_lock_sync(&mdev->use_lock);
239 		snd_midi_event_free(mdev->coder);
240 		kfree(mdev);
241 	}
242 	spin_lock_irqsave(&register_lock, flags);
243 	for (index = max_midi_devs - 1; index >= 0; index--) {
244 		if (midi_devs[index])
245 			break;
246 	}
247 	max_midi_devs = index + 1;
248 	spin_unlock_irqrestore(&register_lock, flags);
249 	return 0;
250 }
251 
252 
253 /*
254  * release the midi device if it was registered
255  */
256 void
snd_seq_oss_midi_clear_all(void)257 snd_seq_oss_midi_clear_all(void)
258 {
259 	int i;
260 	struct seq_oss_midi *mdev;
261 	unsigned long flags;
262 
263 	spin_lock_irqsave(&register_lock, flags);
264 	for (i = 0; i < max_midi_devs; i++) {
265 		if ((mdev = midi_devs[i]) != NULL) {
266 			snd_midi_event_free(mdev->coder);
267 			kfree(mdev);
268 			midi_devs[i] = NULL;
269 		}
270 	}
271 	max_midi_devs = 0;
272 	spin_unlock_irqrestore(&register_lock, flags);
273 }
274 
275 
276 /*
277  * set up midi tables
278  */
279 void
snd_seq_oss_midi_setup(struct seq_oss_devinfo * dp)280 snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
281 {
282 	dp->max_mididev = max_midi_devs;
283 }
284 
285 /*
286  * clean up midi tables
287  */
288 void
snd_seq_oss_midi_cleanup(struct seq_oss_devinfo * dp)289 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
290 {
291 	int i;
292 	for (i = 0; i < dp->max_mididev; i++)
293 		snd_seq_oss_midi_close(dp, i);
294 	dp->max_mididev = 0;
295 }
296 
297 
298 /*
299  * open all midi devices.  ignore errors.
300  */
301 void
snd_seq_oss_midi_open_all(struct seq_oss_devinfo * dp,int file_mode)302 snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
303 {
304 	int i;
305 	for (i = 0; i < dp->max_mididev; i++)
306 		snd_seq_oss_midi_open(dp, i, file_mode);
307 }
308 
309 
310 /*
311  * get the midi device information
312  */
313 static struct seq_oss_midi *
get_mididev(struct seq_oss_devinfo * dp,int dev)314 get_mididev(struct seq_oss_devinfo *dp, int dev)
315 {
316 	if (dev < 0 || dev >= dp->max_mididev)
317 		return NULL;
318 	return get_mdev(dev);
319 }
320 
321 
322 /*
323  * open the midi device if not opened yet
324  */
325 int
snd_seq_oss_midi_open(struct seq_oss_devinfo * dp,int dev,int fmode)326 snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
327 {
328 	int perm;
329 	struct seq_oss_midi *mdev;
330 	struct snd_seq_port_subscribe subs;
331 
332 	if ((mdev = get_mididev(dp, dev)) == NULL)
333 		return -ENODEV;
334 
335 	/* already used? */
336 	if (mdev->opened && mdev->devinfo != dp) {
337 		snd_use_lock_free(&mdev->use_lock);
338 		return -EBUSY;
339 	}
340 
341 	perm = 0;
342 	if (is_write_mode(fmode))
343 		perm |= PERM_WRITE;
344 	if (is_read_mode(fmode))
345 		perm |= PERM_READ;
346 	perm &= mdev->flags;
347 	if (perm == 0) {
348 		snd_use_lock_free(&mdev->use_lock);
349 		return -ENXIO;
350 	}
351 
352 	/* already opened? */
353 	if ((mdev->opened & perm) == perm) {
354 		snd_use_lock_free(&mdev->use_lock);
355 		return 0;
356 	}
357 
358 	perm &= ~mdev->opened;
359 
360 	memset(&subs, 0, sizeof(subs));
361 
362 	if (perm & PERM_WRITE) {
363 		subs.sender = dp->addr;
364 		subs.dest.client = mdev->client;
365 		subs.dest.port = mdev->port;
366 		if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
367 			mdev->opened |= PERM_WRITE;
368 	}
369 	if (perm & PERM_READ) {
370 		subs.sender.client = mdev->client;
371 		subs.sender.port = mdev->port;
372 		subs.dest = dp->addr;
373 		subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
374 		subs.queue = dp->queue;		/* queue for timestamps */
375 		if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
376 			mdev->opened |= PERM_READ;
377 	}
378 
379 	if (! mdev->opened) {
380 		snd_use_lock_free(&mdev->use_lock);
381 		return -ENXIO;
382 	}
383 
384 	mdev->devinfo = dp;
385 	snd_use_lock_free(&mdev->use_lock);
386 	return 0;
387 }
388 
389 /*
390  * close the midi device if already opened
391  */
392 int
snd_seq_oss_midi_close(struct seq_oss_devinfo * dp,int dev)393 snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
394 {
395 	struct seq_oss_midi *mdev;
396 	struct snd_seq_port_subscribe subs;
397 
398 	if ((mdev = get_mididev(dp, dev)) == NULL)
399 		return -ENODEV;
400 	if (! mdev->opened || mdev->devinfo != dp) {
401 		snd_use_lock_free(&mdev->use_lock);
402 		return 0;
403 	}
404 
405 	memset(&subs, 0, sizeof(subs));
406 	if (mdev->opened & PERM_WRITE) {
407 		subs.sender = dp->addr;
408 		subs.dest.client = mdev->client;
409 		subs.dest.port = mdev->port;
410 		snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
411 	}
412 	if (mdev->opened & PERM_READ) {
413 		subs.sender.client = mdev->client;
414 		subs.sender.port = mdev->port;
415 		subs.dest = dp->addr;
416 		snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
417 	}
418 
419 	mdev->opened = 0;
420 	mdev->devinfo = NULL;
421 
422 	snd_use_lock_free(&mdev->use_lock);
423 	return 0;
424 }
425 
426 /*
427  * change seq capability flags to file mode flags
428  */
429 int
snd_seq_oss_midi_filemode(struct seq_oss_devinfo * dp,int dev)430 snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
431 {
432 	struct seq_oss_midi *mdev;
433 	int mode;
434 
435 	if ((mdev = get_mididev(dp, dev)) == NULL)
436 		return 0;
437 
438 	mode = 0;
439 	if (mdev->opened & PERM_WRITE)
440 		mode |= SNDRV_SEQ_OSS_FILE_WRITE;
441 	if (mdev->opened & PERM_READ)
442 		mode |= SNDRV_SEQ_OSS_FILE_READ;
443 
444 	snd_use_lock_free(&mdev->use_lock);
445 	return mode;
446 }
447 
448 /*
449  * reset the midi device and close it:
450  * so far, only close the device.
451  */
452 void
snd_seq_oss_midi_reset(struct seq_oss_devinfo * dp,int dev)453 snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
454 {
455 	struct seq_oss_midi *mdev;
456 
457 	if ((mdev = get_mididev(dp, dev)) == NULL)
458 		return;
459 	if (! mdev->opened) {
460 		snd_use_lock_free(&mdev->use_lock);
461 		return;
462 	}
463 
464 	if (mdev->opened & PERM_WRITE) {
465 		struct snd_seq_event ev;
466 		int c;
467 
468 		memset(&ev, 0, sizeof(ev));
469 		ev.dest.client = mdev->client;
470 		ev.dest.port = mdev->port;
471 		ev.queue = dp->queue;
472 		ev.source.port = dp->port;
473 		if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
474 			ev.type = SNDRV_SEQ_EVENT_SENSING;
475 			snd_seq_oss_dispatch(dp, &ev, 0, 0);
476 		}
477 		for (c = 0; c < 16; c++) {
478 			ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
479 			ev.data.control.channel = c;
480 			ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
481 			snd_seq_oss_dispatch(dp, &ev, 0, 0);
482 			if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
483 				ev.data.control.param =
484 					MIDI_CTL_RESET_CONTROLLERS;
485 				snd_seq_oss_dispatch(dp, &ev, 0, 0);
486 				ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
487 				ev.data.control.value = 0;
488 				snd_seq_oss_dispatch(dp, &ev, 0, 0);
489 			}
490 		}
491 	}
492 	// snd_seq_oss_midi_close(dp, dev);
493 	snd_use_lock_free(&mdev->use_lock);
494 }
495 
496 
497 /*
498  * get client/port of the specified MIDI device
499  */
500 void
snd_seq_oss_midi_get_addr(struct seq_oss_devinfo * dp,int dev,struct snd_seq_addr * addr)501 snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
502 {
503 	struct seq_oss_midi *mdev;
504 
505 	if ((mdev = get_mididev(dp, dev)) == NULL)
506 		return;
507 	addr->client = mdev->client;
508 	addr->port = mdev->port;
509 	snd_use_lock_free(&mdev->use_lock);
510 }
511 
512 
513 /*
514  * input callback - this can be atomic
515  */
516 int
snd_seq_oss_midi_input(struct snd_seq_event * ev,int direct,void * private_data)517 snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
518 {
519 	struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
520 	struct seq_oss_midi *mdev;
521 	int rc;
522 
523 	if (dp->readq == NULL)
524 		return 0;
525 	if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
526 		return 0;
527 	if (! (mdev->opened & PERM_READ)) {
528 		snd_use_lock_free(&mdev->use_lock);
529 		return 0;
530 	}
531 
532 	if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
533 		rc = send_synth_event(dp, ev, mdev->seq_device);
534 	else
535 		rc = send_midi_event(dp, ev, mdev);
536 
537 	snd_use_lock_free(&mdev->use_lock);
538 	return rc;
539 }
540 
541 /*
542  * convert ALSA sequencer event to OSS synth event
543  */
544 static int
send_synth_event(struct seq_oss_devinfo * dp,struct snd_seq_event * ev,int dev)545 send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
546 {
547 	union evrec ossev;
548 
549 	memset(&ossev, 0, sizeof(ossev));
550 
551 	switch (ev->type) {
552 	case SNDRV_SEQ_EVENT_NOTEON:
553 		ossev.v.cmd = MIDI_NOTEON; break;
554 	case SNDRV_SEQ_EVENT_NOTEOFF:
555 		ossev.v.cmd = MIDI_NOTEOFF; break;
556 	case SNDRV_SEQ_EVENT_KEYPRESS:
557 		ossev.v.cmd = MIDI_KEY_PRESSURE; break;
558 	case SNDRV_SEQ_EVENT_CONTROLLER:
559 		ossev.l.cmd = MIDI_CTL_CHANGE; break;
560 	case SNDRV_SEQ_EVENT_PGMCHANGE:
561 		ossev.l.cmd = MIDI_PGM_CHANGE; break;
562 	case SNDRV_SEQ_EVENT_CHANPRESS:
563 		ossev.l.cmd = MIDI_CHN_PRESSURE; break;
564 	case SNDRV_SEQ_EVENT_PITCHBEND:
565 		ossev.l.cmd = MIDI_PITCH_BEND; break;
566 	default:
567 		return 0; /* not supported */
568 	}
569 
570 	ossev.v.dev = dev;
571 
572 	switch (ev->type) {
573 	case SNDRV_SEQ_EVENT_NOTEON:
574 	case SNDRV_SEQ_EVENT_NOTEOFF:
575 	case SNDRV_SEQ_EVENT_KEYPRESS:
576 		ossev.v.code = EV_CHN_VOICE;
577 		ossev.v.note = ev->data.note.note;
578 		ossev.v.parm = ev->data.note.velocity;
579 		ossev.v.chn = ev->data.note.channel;
580 		break;
581 	case SNDRV_SEQ_EVENT_CONTROLLER:
582 	case SNDRV_SEQ_EVENT_PGMCHANGE:
583 	case SNDRV_SEQ_EVENT_CHANPRESS:
584 		ossev.l.code = EV_CHN_COMMON;
585 		ossev.l.p1 = ev->data.control.param;
586 		ossev.l.val = ev->data.control.value;
587 		ossev.l.chn = ev->data.control.channel;
588 		break;
589 	case SNDRV_SEQ_EVENT_PITCHBEND:
590 		ossev.l.code = EV_CHN_COMMON;
591 		ossev.l.val = ev->data.control.value + 8192;
592 		ossev.l.chn = ev->data.control.channel;
593 		break;
594 	}
595 
596 	snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
597 	snd_seq_oss_readq_put_event(dp->readq, &ossev);
598 
599 	return 0;
600 }
601 
602 /*
603  * decode event and send MIDI bytes to read queue
604  */
605 static int
send_midi_event(struct seq_oss_devinfo * dp,struct snd_seq_event * ev,struct seq_oss_midi * mdev)606 send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
607 {
608 	char msg[32];
609 	int len;
610 
611 	snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
612 	if (!dp->timer->running)
613 		len = snd_seq_oss_timer_start(dp->timer);
614 	if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
615 		if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
616 			snd_seq_oss_readq_puts(dp->readq, mdev->seq_device,
617 					       ev->data.ext.ptr, ev->data.ext.len);
618 	} else {
619 		len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
620 		if (len > 0)
621 			snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
622 	}
623 
624 	return 0;
625 }
626 
627 
628 /*
629  * dump midi data
630  * return 0 : enqueued
631  *        non-zero : invalid - ignored
632  */
633 int
snd_seq_oss_midi_putc(struct seq_oss_devinfo * dp,int dev,unsigned char c,struct snd_seq_event * ev)634 snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
635 {
636 	struct seq_oss_midi *mdev;
637 
638 	if ((mdev = get_mididev(dp, dev)) == NULL)
639 		return -ENODEV;
640 	if (snd_midi_event_encode_byte(mdev->coder, c, ev) > 0) {
641 		snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port);
642 		snd_use_lock_free(&mdev->use_lock);
643 		return 0;
644 	}
645 	snd_use_lock_free(&mdev->use_lock);
646 	return -EINVAL;
647 }
648 
649 /*
650  * create OSS compatible midi_info record
651  */
652 int
snd_seq_oss_midi_make_info(struct seq_oss_devinfo * dp,int dev,struct midi_info * inf)653 snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf)
654 {
655 	struct seq_oss_midi *mdev;
656 
657 	if ((mdev = get_mididev(dp, dev)) == NULL)
658 		return -ENXIO;
659 	inf->device = dev;
660 	inf->dev_type = 0; /* FIXME: ?? */
661 	inf->capabilities = 0; /* FIXME: ?? */
662 	strlcpy(inf->name, mdev->name, sizeof(inf->name));
663 	snd_use_lock_free(&mdev->use_lock);
664 	return 0;
665 }
666 
667 
668 #ifdef CONFIG_SND_PROC_FS
669 /*
670  * proc interface
671  */
672 static char *
capmode_str(int val)673 capmode_str(int val)
674 {
675 	val &= PERM_READ|PERM_WRITE;
676 	if (val == (PERM_READ|PERM_WRITE))
677 		return "read/write";
678 	else if (val == PERM_READ)
679 		return "read";
680 	else if (val == PERM_WRITE)
681 		return "write";
682 	else
683 		return "none";
684 }
685 
686 void
snd_seq_oss_midi_info_read(struct snd_info_buffer * buf)687 snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
688 {
689 	int i;
690 	struct seq_oss_midi *mdev;
691 
692 	snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
693 	for (i = 0; i < max_midi_devs; i++) {
694 		snd_iprintf(buf, "\nmidi %d: ", i);
695 		mdev = get_mdev(i);
696 		if (mdev == NULL) {
697 			snd_iprintf(buf, "*empty*\n");
698 			continue;
699 		}
700 		snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
701 			    mdev->client, mdev->port);
702 		snd_iprintf(buf, "  capability %s / opened %s\n",
703 			    capmode_str(mdev->flags),
704 			    capmode_str(mdev->opened));
705 		snd_use_lock_free(&mdev->use_lock);
706 	}
707 }
708 #endif /* CONFIG_SND_PROC_FS */
709