1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include "seq_oss_synth.h"
24#include "seq_oss_midi.h"
25#include "../seq_lock.h"
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/slab.h>
29
30
31
32
33#define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
34#define MAX_SYSEX_BUFLEN 128
35
36
37
38
39
40
41
42struct seq_oss_synth_sysex {
43 int len;
44 int skip;
45 unsigned char buf[MAX_SYSEX_BUFLEN];
46};
47
48
49struct seq_oss_synth {
50 int seq_device;
51
52
53 int synth_type;
54 int synth_subtype;
55 int nr_voices;
56
57 char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
58 struct snd_seq_oss_callback oper;
59
60 int opened;
61
62 void *private_data;
63 snd_use_lock_t use_lock;
64};
65
66
67
68
69
70static int max_synth_devs;
71static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
72static struct seq_oss_synth midi_synth_dev = {
73 -1,
74 SYNTH_TYPE_MIDI,
75 0,
76 16,
77 "MIDI",
78};
79
80static DEFINE_SPINLOCK(register_lock);
81
82
83
84
85static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
86static void reset_channels(struct seq_oss_synthinfo *info);
87
88
89
90
91void __init
92snd_seq_oss_synth_init(void)
93{
94 snd_use_lock_init(&midi_synth_dev.use_lock);
95}
96
97
98
99
100int
101snd_seq_oss_synth_register(struct snd_seq_device *dev)
102{
103 int i;
104 struct seq_oss_synth *rec;
105 struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
106 unsigned long flags;
107
108 if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL) {
109 pr_err("ALSA: seq_oss: can't malloc synth info\n");
110 return -ENOMEM;
111 }
112 rec->seq_device = -1;
113 rec->synth_type = reg->type;
114 rec->synth_subtype = reg->subtype;
115 rec->nr_voices = reg->nvoices;
116 rec->oper = reg->oper;
117 rec->private_data = reg->private_data;
118 rec->opened = 0;
119 snd_use_lock_init(&rec->use_lock);
120
121
122 strlcpy(rec->name, dev->name, sizeof(rec->name));
123
124
125 spin_lock_irqsave(®ister_lock, flags);
126 for (i = 0; i < max_synth_devs; i++) {
127 if (synth_devs[i] == NULL)
128 break;
129 }
130 if (i >= max_synth_devs) {
131 if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) {
132 spin_unlock_irqrestore(®ister_lock, flags);
133 pr_err("ALSA: seq_oss: no more synth slot\n");
134 kfree(rec);
135 return -ENOMEM;
136 }
137 max_synth_devs++;
138 }
139 rec->seq_device = i;
140 synth_devs[i] = rec;
141 spin_unlock_irqrestore(®ister_lock, flags);
142 dev->driver_data = rec;
143#ifdef SNDRV_OSS_INFO_DEV_SYNTH
144 if (i < SNDRV_CARDS)
145 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name);
146#endif
147 return 0;
148}
149
150
151int
152snd_seq_oss_synth_unregister(struct snd_seq_device *dev)
153{
154 int index;
155 struct seq_oss_synth *rec = dev->driver_data;
156 unsigned long flags;
157
158 spin_lock_irqsave(®ister_lock, flags);
159 for (index = 0; index < max_synth_devs; index++) {
160 if (synth_devs[index] == rec)
161 break;
162 }
163 if (index >= max_synth_devs) {
164 spin_unlock_irqrestore(®ister_lock, flags);
165 pr_err("ALSA: seq_oss: can't unregister synth\n");
166 return -EINVAL;
167 }
168 synth_devs[index] = NULL;
169 if (index == max_synth_devs - 1) {
170 for (index--; index >= 0; index--) {
171 if (synth_devs[index])
172 break;
173 }
174 max_synth_devs = index + 1;
175 }
176 spin_unlock_irqrestore(®ister_lock, flags);
177#ifdef SNDRV_OSS_INFO_DEV_SYNTH
178 if (rec->seq_device < SNDRV_CARDS)
179 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device);
180#endif
181
182 snd_use_lock_sync(&rec->use_lock);
183 kfree(rec);
184
185 return 0;
186}
187
188
189
190
191static struct seq_oss_synth *
192get_sdev(int dev)
193{
194 struct seq_oss_synth *rec;
195 unsigned long flags;
196
197 spin_lock_irqsave(®ister_lock, flags);
198 rec = synth_devs[dev];
199 if (rec)
200 snd_use_lock_use(&rec->use_lock);
201 spin_unlock_irqrestore(®ister_lock, flags);
202 return rec;
203}
204
205
206
207
208
209
210void
211snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
212{
213 int i;
214 struct seq_oss_synth *rec;
215 struct seq_oss_synthinfo *info;
216
217 dp->max_synthdev = max_synth_devs;
218 dp->synth_opened = 0;
219 memset(dp->synths, 0, sizeof(dp->synths));
220 for (i = 0; i < dp->max_synthdev; i++) {
221 rec = get_sdev(i);
222 if (rec == NULL)
223 continue;
224 if (rec->oper.open == NULL || rec->oper.close == NULL) {
225 snd_use_lock_free(&rec->use_lock);
226 continue;
227 }
228 info = &dp->synths[i];
229 info->arg.app_index = dp->port;
230 info->arg.file_mode = dp->file_mode;
231 info->arg.seq_mode = dp->seq_mode;
232 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
233 info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
234 else
235 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
236 info->opened = 0;
237 if (!try_module_get(rec->oper.owner)) {
238 snd_use_lock_free(&rec->use_lock);
239 continue;
240 }
241 if (rec->oper.open(&info->arg, rec->private_data) < 0) {
242 module_put(rec->oper.owner);
243 snd_use_lock_free(&rec->use_lock);
244 continue;
245 }
246 info->nr_voices = rec->nr_voices;
247 if (info->nr_voices > 0) {
248 info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL);
249 if (!info->ch) {
250 pr_err("ALSA: seq_oss: Cannot malloc voices\n");
251 rec->oper.close(&info->arg);
252 module_put(rec->oper.owner);
253 snd_use_lock_free(&rec->use_lock);
254 continue;
255 }
256 reset_channels(info);
257 }
258 info->opened++;
259 rec->opened++;
260 dp->synth_opened++;
261 snd_use_lock_free(&rec->use_lock);
262 }
263}
264
265
266
267
268
269
270void
271snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp)
272{
273 int i;
274
275 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
276 return;
277
278 for (i = 0; i < dp->max_mididev; i++) {
279 struct seq_oss_synthinfo *info;
280 info = &dp->synths[dp->max_synthdev];
281 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
282 continue;
283 info->arg.app_index = dp->port;
284 info->arg.file_mode = dp->file_mode;
285 info->arg.seq_mode = dp->seq_mode;
286 info->arg.private_data = info;
287 info->is_midi = 1;
288 info->midi_mapped = i;
289 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
290 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
291 info->opened = 1;
292 midi_synth_dev.opened++;
293 dp->max_synthdev++;
294 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
295 break;
296 }
297}
298
299
300
301
302
303
304void
305snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp)
306{
307 int i;
308 struct seq_oss_synth *rec;
309 struct seq_oss_synthinfo *info;
310
311 if (snd_BUG_ON(dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS))
312 return;
313 for (i = 0; i < dp->max_synthdev; i++) {
314 info = &dp->synths[i];
315 if (! info->opened)
316 continue;
317 if (info->is_midi) {
318 if (midi_synth_dev.opened > 0) {
319 snd_seq_oss_midi_close(dp, info->midi_mapped);
320 midi_synth_dev.opened--;
321 }
322 } else {
323 rec = get_sdev(i);
324 if (rec == NULL)
325 continue;
326 if (rec->opened > 0) {
327 rec->oper.close(&info->arg);
328 module_put(rec->oper.owner);
329 rec->opened = 0;
330 }
331 snd_use_lock_free(&rec->use_lock);
332 }
333 kfree(info->sysex);
334 info->sysex = NULL;
335 kfree(info->ch);
336 info->ch = NULL;
337 }
338 dp->synth_opened = 0;
339 dp->max_synthdev = 0;
340}
341
342
343
344
345static int
346is_midi_dev(struct seq_oss_devinfo *dp, int dev)
347{
348 if (dev < 0 || dev >= dp->max_synthdev)
349 return 0;
350 if (dp->synths[dev].is_midi)
351 return 1;
352 return 0;
353}
354
355
356
357
358static struct seq_oss_synth *
359get_synthdev(struct seq_oss_devinfo *dp, int dev)
360{
361 struct seq_oss_synth *rec;
362 if (dev < 0 || dev >= dp->max_synthdev)
363 return NULL;
364 if (! dp->synths[dev].opened)
365 return NULL;
366 if (dp->synths[dev].is_midi)
367 return &midi_synth_dev;
368 if ((rec = get_sdev(dev)) == NULL)
369 return NULL;
370 if (! rec->opened) {
371 snd_use_lock_free(&rec->use_lock);
372 return NULL;
373 }
374 return rec;
375}
376
377
378
379
380
381static void
382reset_channels(struct seq_oss_synthinfo *info)
383{
384 int i;
385 if (info->ch == NULL || ! info->nr_voices)
386 return;
387 for (i = 0; i < info->nr_voices; i++) {
388 info->ch[i].note = -1;
389 info->ch[i].vel = 0;
390 }
391}
392
393
394
395
396
397
398
399void
400snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
401{
402 struct seq_oss_synth *rec;
403 struct seq_oss_synthinfo *info;
404
405 if (snd_BUG_ON(dev < 0 || dev >= dp->max_synthdev))
406 return;
407 info = &dp->synths[dev];
408 if (! info->opened)
409 return;
410 if (info->sysex)
411 info->sysex->len = 0;
412 reset_channels(info);
413 if (info->is_midi) {
414 if (midi_synth_dev.opened <= 0)
415 return;
416 snd_seq_oss_midi_reset(dp, info->midi_mapped);
417
418 snd_seq_oss_midi_close(dp, dev);
419 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
420 dp->file_mode) < 0) {
421 midi_synth_dev.opened--;
422 info->opened = 0;
423 kfree(info->sysex);
424 info->sysex = NULL;
425 kfree(info->ch);
426 info->ch = NULL;
427 }
428 return;
429 }
430
431 rec = get_sdev(dev);
432 if (rec == NULL)
433 return;
434 if (rec->oper.reset) {
435 rec->oper.reset(&info->arg);
436 } else {
437 struct snd_seq_event ev;
438 memset(&ev, 0, sizeof(ev));
439 snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client,
440 info->arg.addr.port);
441 ev.type = SNDRV_SEQ_EVENT_RESET;
442 snd_seq_oss_dispatch(dp, &ev, 0, 0);
443 }
444 snd_use_lock_free(&rec->use_lock);
445}
446
447
448
449
450
451
452int
453snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt,
454 const char __user *buf, int p, int c)
455{
456 struct seq_oss_synth *rec;
457 int rc;
458
459 if (dev < 0 || dev >= dp->max_synthdev)
460 return -ENXIO;
461
462 if (is_midi_dev(dp, dev))
463 return 0;
464 if ((rec = get_synthdev(dp, dev)) == NULL)
465 return -ENXIO;
466
467 if (rec->oper.load_patch == NULL)
468 rc = -ENXIO;
469 else
470 rc = rec->oper.load_patch(&dp->synths[dev].arg, fmt, buf, p, c);
471 snd_use_lock_free(&rec->use_lock);
472 return rc;
473}
474
475
476
477
478int
479snd_seq_oss_synth_is_valid(struct seq_oss_devinfo *dp, int dev)
480{
481 struct seq_oss_synth *rec;
482 rec = get_synthdev(dp, dev);
483 if (rec) {
484 snd_use_lock_free(&rec->use_lock);
485 return 1;
486 }
487 return 0;
488}
489
490
491
492
493
494
495
496int
497snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
498{
499 int i, send;
500 unsigned char *dest;
501 struct seq_oss_synth_sysex *sysex;
502
503 if (! snd_seq_oss_synth_is_valid(dp, dev))
504 return -ENXIO;
505
506 sysex = dp->synths[dev].sysex;
507 if (sysex == NULL) {
508 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
509 if (sysex == NULL)
510 return -ENOMEM;
511 dp->synths[dev].sysex = sysex;
512 }
513
514 send = 0;
515 dest = sysex->buf + sysex->len;
516
517 for (i = 0; i < 6; i++) {
518 if (buf[i] == 0xff) {
519 send = 1;
520 break;
521 }
522 dest[i] = buf[i];
523 sysex->len++;
524 if (sysex->len >= MAX_SYSEX_BUFLEN) {
525 sysex->len = 0;
526 sysex->skip = 1;
527 break;
528 }
529 }
530
531 if (sysex->len && send) {
532 if (sysex->skip) {
533 sysex->skip = 0;
534 sysex->len = 0;
535 return -EINVAL;
536 }
537
538 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
539 if (snd_seq_oss_synth_addr(dp, dev, ev))
540 return -EINVAL;
541 ev->data.ext.len = sysex->len;
542 ev->data.ext.ptr = sysex->buf;
543 sysex->len = 0;
544 return 0;
545 }
546
547 return -EINVAL;
548}
549
550
551
552
553int
554snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
555{
556 if (! snd_seq_oss_synth_is_valid(dp, dev))
557 return -EINVAL;
558 snd_seq_oss_fill_addr(dp, ev, dp->synths[dev].arg.addr.client,
559 dp->synths[dev].arg.addr.port);
560 return 0;
561}
562
563
564
565
566
567int
568snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
569{
570 struct seq_oss_synth *rec;
571 int rc;
572
573 if (is_midi_dev(dp, dev))
574 return -ENXIO;
575 if ((rec = get_synthdev(dp, dev)) == NULL)
576 return -ENXIO;
577 if (rec->oper.ioctl == NULL)
578 rc = -ENXIO;
579 else
580 rc = rec->oper.ioctl(&dp->synths[dev].arg, cmd, addr);
581 snd_use_lock_free(&rec->use_lock);
582 return rc;
583}
584
585
586
587
588
589int
590snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
591{
592 if (! snd_seq_oss_synth_is_valid(dp, dev) || is_midi_dev(dp, dev))
593 return -ENXIO;
594 ev->type = SNDRV_SEQ_EVENT_OSS;
595 memcpy(ev->data.raw8.d, data, 8);
596 return snd_seq_oss_synth_addr(dp, dev, ev);
597}
598
599
600
601
602
603int
604snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
605{
606 struct seq_oss_synth *rec;
607
608 if (dev < 0 || dev >= dp->max_synthdev)
609 return -ENXIO;
610
611 if (dp->synths[dev].is_midi) {
612 struct midi_info minf;
613 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
614 inf->synth_type = SYNTH_TYPE_MIDI;
615 inf->synth_subtype = 0;
616 inf->nr_voices = 16;
617 inf->device = dev;
618 strlcpy(inf->name, minf.name, sizeof(inf->name));
619 } else {
620 if ((rec = get_synthdev(dp, dev)) == NULL)
621 return -ENXIO;
622 inf->synth_type = rec->synth_type;
623 inf->synth_subtype = rec->synth_subtype;
624 inf->nr_voices = rec->nr_voices;
625 inf->device = dev;
626 strlcpy(inf->name, rec->name, sizeof(inf->name));
627 snd_use_lock_free(&rec->use_lock);
628 }
629 return 0;
630}
631
632
633#ifdef CONFIG_PROC_FS
634
635
636
637void
638snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
639{
640 int i;
641 struct seq_oss_synth *rec;
642
643 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
644 for (i = 0; i < max_synth_devs; i++) {
645 snd_iprintf(buf, "\nsynth %d: ", i);
646 rec = get_sdev(i);
647 if (rec == NULL) {
648 snd_iprintf(buf, "*empty*\n");
649 continue;
650 }
651 snd_iprintf(buf, "[%s]\n", rec->name);
652 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
653 rec->synth_type, rec->synth_subtype,
654 rec->nr_voices);
655 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
656 enabled_str((long)rec->oper.ioctl),
657 enabled_str((long)rec->oper.load_patch));
658 snd_use_lock_free(&rec->use_lock);
659 }
660}
661#endif
662