1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
36
37#define SNDRV_SEQ_OSS_MAX_MIDI_NAME 30
38
39
40
41
42struct seq_oss_midi {
43 int seq_device;
44 int client;
45 int port;
46 unsigned int flags;
47 int opened;
48 unsigned char name[SNDRV_SEQ_OSS_MAX_MIDI_NAME];
49 struct snd_midi_event *coder;
50 struct seq_oss_devinfo *devinfo;
51 snd_use_lock_t use_lock;
52};
53
54
55
56
57
58static int max_midi_devs;
59static struct seq_oss_midi *midi_devs[SNDRV_SEQ_OSS_MAX_MIDI_DEVS];
60
61static DEFINE_SPINLOCK(register_lock);
62
63
64
65
66static struct seq_oss_midi *get_mdev(int dev);
67static struct seq_oss_midi *get_mididev(struct seq_oss_devinfo *dp, int dev);
68static int send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev);
69static int send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev);
70
71
72
73
74
75int
76snd_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;
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
105static struct seq_oss_midi *
106get_mdev(int dev)
107{
108 struct seq_oss_midi *mdev;
109 unsigned long flags;
110
111 spin_lock_irqsave(®ister_lock, flags);
112 mdev = midi_devs[dev];
113 if (mdev)
114 snd_use_lock_use(&mdev->use_lock);
115 spin_unlock_irqrestore(®ister_lock, flags);
116 return mdev;
117}
118
119
120
121
122static struct seq_oss_midi *
123find_slot(int client, int port)
124{
125 int i;
126 struct seq_oss_midi *mdev;
127 unsigned long flags;
128
129 spin_lock_irqsave(®ister_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
134 snd_use_lock_use(&mdev->use_lock);
135 spin_unlock_irqrestore(®ister_lock, flags);
136 return mdev;
137 }
138 }
139 spin_unlock_irqrestore(®ister_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
148
149int
150snd_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
157 if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
158 return 0;
159
160 if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
161 (pinfo->capability & PERM_READ) != PERM_READ)
162 return 0;
163
164
165
166
167 if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
168
169 snd_use_lock_free(&mdev->use_lock);
170 return 0;
171 }
172
173
174
175
176 if ((mdev = kzalloc(sizeof(*mdev), GFP_KERNEL)) == NULL) {
177 pr_err("ALSA: seq_oss: can't malloc midi info\n");
178 return -ENOMEM;
179 }
180
181
182 mdev->client = pinfo->addr.client;
183 mdev->port = pinfo->addr.port;
184 mdev->flags = pinfo->capability;
185 mdev->opened = 0;
186 snd_use_lock_init(&mdev->use_lock);
187
188
189 strlcpy(mdev->name, pinfo->name, sizeof(mdev->name));
190
191
192 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
193 pr_err("ALSA: seq_oss: can't malloc midi coder\n");
194 kfree(mdev);
195 return -ENOMEM;
196 }
197
198 snd_midi_event_no_status(mdev->coder, 1);
199
200
201
202
203 spin_lock_irqsave(®ister_lock, flags);
204 for (i = 0; i < max_midi_devs; i++) {
205 if (midi_devs[i] == NULL)
206 break;
207 }
208 if (i >= max_midi_devs) {
209 if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
210 spin_unlock_irqrestore(®ister_lock, flags);
211 snd_midi_event_free(mdev->coder);
212 kfree(mdev);
213 return -ENOMEM;
214 }
215 max_midi_devs++;
216 }
217 mdev->seq_device = i;
218 midi_devs[mdev->seq_device] = mdev;
219 spin_unlock_irqrestore(®ister_lock, flags);
220
221 return 0;
222}
223
224
225
226
227int
228snd_seq_oss_midi_check_exit_port(int client, int port)
229{
230 struct seq_oss_midi *mdev;
231 unsigned long flags;
232 int index;
233
234 if ((mdev = find_slot(client, port)) != NULL) {
235 spin_lock_irqsave(®ister_lock, flags);
236 midi_devs[mdev->seq_device] = NULL;
237 spin_unlock_irqrestore(®ister_lock, flags);
238 snd_use_lock_free(&mdev->use_lock);
239 snd_use_lock_sync(&mdev->use_lock);
240 snd_midi_event_free(mdev->coder);
241 kfree(mdev);
242 }
243 spin_lock_irqsave(®ister_lock, flags);
244 for (index = max_midi_devs - 1; index >= 0; index--) {
245 if (midi_devs[index])
246 break;
247 }
248 max_midi_devs = index + 1;
249 spin_unlock_irqrestore(®ister_lock, flags);
250 return 0;
251}
252
253
254
255
256
257void
258snd_seq_oss_midi_clear_all(void)
259{
260 int i;
261 struct seq_oss_midi *mdev;
262 unsigned long flags;
263
264 spin_lock_irqsave(®ister_lock, flags);
265 for (i = 0; i < max_midi_devs; i++) {
266 if ((mdev = midi_devs[i]) != NULL) {
267 snd_midi_event_free(mdev->coder);
268 kfree(mdev);
269 midi_devs[i] = NULL;
270 }
271 }
272 max_midi_devs = 0;
273 spin_unlock_irqrestore(®ister_lock, flags);
274}
275
276
277
278
279
280void
281snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
282{
283 dp->max_mididev = max_midi_devs;
284}
285
286
287
288
289void
290snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
291{
292 int i;
293 for (i = 0; i < dp->max_mididev; i++)
294 snd_seq_oss_midi_close(dp, i);
295 dp->max_mididev = 0;
296}
297
298
299
300
301
302void
303snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
304{
305 int i;
306 for (i = 0; i < dp->max_mididev; i++)
307 snd_seq_oss_midi_open(dp, i, file_mode);
308}
309
310
311
312
313
314static struct seq_oss_midi *
315get_mididev(struct seq_oss_devinfo *dp, int dev)
316{
317 if (dev < 0 || dev >= dp->max_mididev)
318 return NULL;
319 return get_mdev(dev);
320}
321
322
323
324
325
326int
327snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
328{
329 int perm;
330 struct seq_oss_midi *mdev;
331 struct snd_seq_port_subscribe subs;
332
333 if ((mdev = get_mididev(dp, dev)) == NULL)
334 return -ENODEV;
335
336
337 if (mdev->opened && mdev->devinfo != dp) {
338 snd_use_lock_free(&mdev->use_lock);
339 return -EBUSY;
340 }
341
342 perm = 0;
343 if (is_write_mode(fmode))
344 perm |= PERM_WRITE;
345 if (is_read_mode(fmode))
346 perm |= PERM_READ;
347 perm &= mdev->flags;
348 if (perm == 0) {
349 snd_use_lock_free(&mdev->use_lock);
350 return -ENXIO;
351 }
352
353
354 if ((mdev->opened & perm) == perm) {
355 snd_use_lock_free(&mdev->use_lock);
356 return 0;
357 }
358
359 perm &= ~mdev->opened;
360
361 memset(&subs, 0, sizeof(subs));
362
363 if (perm & PERM_WRITE) {
364 subs.sender = dp->addr;
365 subs.dest.client = mdev->client;
366 subs.dest.port = mdev->port;
367 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
368 mdev->opened |= PERM_WRITE;
369 }
370 if (perm & PERM_READ) {
371 subs.sender.client = mdev->client;
372 subs.sender.port = mdev->port;
373 subs.dest = dp->addr;
374 subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
375 subs.queue = dp->queue;
376 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
377 mdev->opened |= PERM_READ;
378 }
379
380 if (! mdev->opened) {
381 snd_use_lock_free(&mdev->use_lock);
382 return -ENXIO;
383 }
384
385 mdev->devinfo = dp;
386 snd_use_lock_free(&mdev->use_lock);
387 return 0;
388}
389
390
391
392
393int
394snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
395{
396 struct seq_oss_midi *mdev;
397 struct snd_seq_port_subscribe subs;
398
399 if ((mdev = get_mididev(dp, dev)) == NULL)
400 return -ENODEV;
401 if (! mdev->opened || mdev->devinfo != dp) {
402 snd_use_lock_free(&mdev->use_lock);
403 return 0;
404 }
405
406 memset(&subs, 0, sizeof(subs));
407 if (mdev->opened & PERM_WRITE) {
408 subs.sender = dp->addr;
409 subs.dest.client = mdev->client;
410 subs.dest.port = mdev->port;
411 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
412 }
413 if (mdev->opened & PERM_READ) {
414 subs.sender.client = mdev->client;
415 subs.sender.port = mdev->port;
416 subs.dest = dp->addr;
417 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
418 }
419
420 mdev->opened = 0;
421 mdev->devinfo = NULL;
422
423 snd_use_lock_free(&mdev->use_lock);
424 return 0;
425}
426
427
428
429
430int
431snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
432{
433 struct seq_oss_midi *mdev;
434 int mode;
435
436 if ((mdev = get_mididev(dp, dev)) == NULL)
437 return 0;
438
439 mode = 0;
440 if (mdev->opened & PERM_WRITE)
441 mode |= SNDRV_SEQ_OSS_FILE_WRITE;
442 if (mdev->opened & PERM_READ)
443 mode |= SNDRV_SEQ_OSS_FILE_READ;
444
445 snd_use_lock_free(&mdev->use_lock);
446 return mode;
447}
448
449
450
451
452
453void
454snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
455{
456 struct seq_oss_midi *mdev;
457
458 if ((mdev = get_mididev(dp, dev)) == NULL)
459 return;
460 if (! mdev->opened) {
461 snd_use_lock_free(&mdev->use_lock);
462 return;
463 }
464
465 if (mdev->opened & PERM_WRITE) {
466 struct snd_seq_event ev;
467 int c;
468
469 memset(&ev, 0, sizeof(ev));
470 ev.dest.client = mdev->client;
471 ev.dest.port = mdev->port;
472 ev.queue = dp->queue;
473 ev.source.port = dp->port;
474 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
475 ev.type = SNDRV_SEQ_EVENT_SENSING;
476 snd_seq_oss_dispatch(dp, &ev, 0, 0);
477 }
478 for (c = 0; c < 16; c++) {
479 ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
480 ev.data.control.channel = c;
481 ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
482 snd_seq_oss_dispatch(dp, &ev, 0, 0);
483 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
484 ev.data.control.param =
485 MIDI_CTL_RESET_CONTROLLERS;
486 snd_seq_oss_dispatch(dp, &ev, 0, 0);
487 ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
488 ev.data.control.value = 0;
489 snd_seq_oss_dispatch(dp, &ev, 0, 0);
490 }
491 }
492 }
493
494 snd_use_lock_free(&mdev->use_lock);
495}
496
497
498
499
500
501void
502snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
503{
504 struct seq_oss_midi *mdev;
505
506 if ((mdev = get_mididev(dp, dev)) == NULL)
507 return;
508 addr->client = mdev->client;
509 addr->port = mdev->port;
510 snd_use_lock_free(&mdev->use_lock);
511}
512
513
514
515
516
517int
518snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
519{
520 struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
521 struct seq_oss_midi *mdev;
522 int rc;
523
524 if (dp->readq == NULL)
525 return 0;
526 if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
527 return 0;
528 if (! (mdev->opened & PERM_READ)) {
529 snd_use_lock_free(&mdev->use_lock);
530 return 0;
531 }
532
533 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
534 rc = send_synth_event(dp, ev, mdev->seq_device);
535 else
536 rc = send_midi_event(dp, ev, mdev);
537
538 snd_use_lock_free(&mdev->use_lock);
539 return rc;
540}
541
542
543
544
545static int
546send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
547{
548 union evrec ossev;
549
550 memset(&ossev, 0, sizeof(ossev));
551
552 switch (ev->type) {
553 case SNDRV_SEQ_EVENT_NOTEON:
554 ossev.v.cmd = MIDI_NOTEON; break;
555 case SNDRV_SEQ_EVENT_NOTEOFF:
556 ossev.v.cmd = MIDI_NOTEOFF; break;
557 case SNDRV_SEQ_EVENT_KEYPRESS:
558 ossev.v.cmd = MIDI_KEY_PRESSURE; break;
559 case SNDRV_SEQ_EVENT_CONTROLLER:
560 ossev.l.cmd = MIDI_CTL_CHANGE; break;
561 case SNDRV_SEQ_EVENT_PGMCHANGE:
562 ossev.l.cmd = MIDI_PGM_CHANGE; break;
563 case SNDRV_SEQ_EVENT_CHANPRESS:
564 ossev.l.cmd = MIDI_CHN_PRESSURE; break;
565 case SNDRV_SEQ_EVENT_PITCHBEND:
566 ossev.l.cmd = MIDI_PITCH_BEND; break;
567 default:
568 return 0;
569 }
570
571 ossev.v.dev = dev;
572
573 switch (ev->type) {
574 case SNDRV_SEQ_EVENT_NOTEON:
575 case SNDRV_SEQ_EVENT_NOTEOFF:
576 case SNDRV_SEQ_EVENT_KEYPRESS:
577 ossev.v.code = EV_CHN_VOICE;
578 ossev.v.note = ev->data.note.note;
579 ossev.v.parm = ev->data.note.velocity;
580 ossev.v.chn = ev->data.note.channel;
581 break;
582 case SNDRV_SEQ_EVENT_CONTROLLER:
583 case SNDRV_SEQ_EVENT_PGMCHANGE:
584 case SNDRV_SEQ_EVENT_CHANPRESS:
585 ossev.l.code = EV_CHN_COMMON;
586 ossev.l.p1 = ev->data.control.param;
587 ossev.l.val = ev->data.control.value;
588 ossev.l.chn = ev->data.control.channel;
589 break;
590 case SNDRV_SEQ_EVENT_PITCHBEND:
591 ossev.l.code = EV_CHN_COMMON;
592 ossev.l.val = ev->data.control.value + 8192;
593 ossev.l.chn = ev->data.control.channel;
594 break;
595 }
596
597 snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
598 snd_seq_oss_readq_put_event(dp->readq, &ossev);
599
600 return 0;
601}
602
603
604
605
606static int
607send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
608{
609 char msg[32];
610 int len;
611
612 snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
613 if (!dp->timer->running)
614 len = snd_seq_oss_timer_start(dp->timer);
615 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
616 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
617 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device,
618 ev->data.ext.ptr, ev->data.ext.len);
619 } else {
620 len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
621 if (len > 0)
622 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
623 }
624
625 return 0;
626}
627
628
629
630
631
632
633
634int
635snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
636{
637 struct seq_oss_midi *mdev;
638
639 if ((mdev = get_mididev(dp, dev)) == NULL)
640 return -ENODEV;
641 if (snd_midi_event_encode_byte(mdev->coder, c, ev) > 0) {
642 snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port);
643 snd_use_lock_free(&mdev->use_lock);
644 return 0;
645 }
646 snd_use_lock_free(&mdev->use_lock);
647 return -EINVAL;
648}
649
650
651
652
653int
654snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf)
655{
656 struct seq_oss_midi *mdev;
657
658 if ((mdev = get_mididev(dp, dev)) == NULL)
659 return -ENXIO;
660 inf->device = dev;
661 inf->dev_type = 0;
662 inf->capabilities = 0;
663 strlcpy(inf->name, mdev->name, sizeof(inf->name));
664 snd_use_lock_free(&mdev->use_lock);
665 return 0;
666}
667
668
669#ifdef CONFIG_PROC_FS
670
671
672
673static char *
674capmode_str(int val)
675{
676 val &= PERM_READ|PERM_WRITE;
677 if (val == (PERM_READ|PERM_WRITE))
678 return "read/write";
679 else if (val == PERM_READ)
680 return "read";
681 else if (val == PERM_WRITE)
682 return "write";
683 else
684 return "none";
685}
686
687void
688snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
689{
690 int i;
691 struct seq_oss_midi *mdev;
692
693 snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
694 for (i = 0; i < max_midi_devs; i++) {
695 snd_iprintf(buf, "\nmidi %d: ", i);
696 mdev = get_mdev(i);
697 if (mdev == NULL) {
698 snd_iprintf(buf, "*empty*\n");
699 continue;
700 }
701 snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
702 mdev->client, mdev->port);
703 snd_iprintf(buf, " capability %s / opened %s\n",
704 capmode_str(mdev->flags),
705 capmode_str(mdev->opened));
706 snd_use_lock_free(&mdev->use_lock);
707 }
708}
709#endif
710