Linux 4.16.11
[linux/fpc-iii.git] / sound / core / seq / oss / seq_oss_synth.c
blob278ebb9931225998dd07f0606eeabe289d71aff5
1 /*
2 * OSS compatible sequencer driver
4 * synth device handlers
6 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
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.
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.
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
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 #include <linux/nospec.h>
32 * constants
34 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
35 #define MAX_SYSEX_BUFLEN 128
39 * definition of synth info records
42 /* sysex buffer */
43 struct seq_oss_synth_sysex {
44 int len;
45 int skip;
46 unsigned char buf[MAX_SYSEX_BUFLEN];
49 /* synth info */
50 struct seq_oss_synth {
51 int seq_device;
53 /* for synth_info */
54 int synth_type;
55 int synth_subtype;
56 int nr_voices;
58 char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
59 struct snd_seq_oss_callback oper;
61 int opened;
63 void *private_data;
64 snd_use_lock_t use_lock;
69 * device table
71 static int max_synth_devs;
72 static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
73 static struct seq_oss_synth midi_synth_dev = {
74 .seq_device = -1,
75 .synth_type = SYNTH_TYPE_MIDI,
76 .synth_subtype = 0,
77 .nr_voices = 16,
78 .name = "MIDI",
81 static DEFINE_SPINLOCK(register_lock);
84 * prototypes
86 static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
87 static void reset_channels(struct seq_oss_synthinfo *info);
90 * global initialization
92 void __init
93 snd_seq_oss_synth_init(void)
95 snd_use_lock_init(&midi_synth_dev.use_lock);
99 * registration of the synth device
102 snd_seq_oss_synth_probe(struct device *_dev)
104 struct snd_seq_device *dev = to_seq_dev(_dev);
105 int i;
106 struct seq_oss_synth *rec;
107 struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
108 unsigned long flags;
110 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
111 if (!rec)
112 return -ENOMEM;
113 rec->seq_device = -1;
114 rec->synth_type = reg->type;
115 rec->synth_subtype = reg->subtype;
116 rec->nr_voices = reg->nvoices;
117 rec->oper = reg->oper;
118 rec->private_data = reg->private_data;
119 rec->opened = 0;
120 snd_use_lock_init(&rec->use_lock);
122 /* copy and truncate the name of synth device */
123 strlcpy(rec->name, dev->name, sizeof(rec->name));
125 /* registration */
126 spin_lock_irqsave(&register_lock, flags);
127 for (i = 0; i < max_synth_devs; i++) {
128 if (synth_devs[i] == NULL)
129 break;
131 if (i >= max_synth_devs) {
132 if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) {
133 spin_unlock_irqrestore(&register_lock, flags);
134 pr_err("ALSA: seq_oss: no more synth slot\n");
135 kfree(rec);
136 return -ENOMEM;
138 max_synth_devs++;
140 rec->seq_device = i;
141 synth_devs[i] = rec;
142 spin_unlock_irqrestore(&register_lock, flags);
143 dev->driver_data = rec;
144 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
145 if (i < SNDRV_CARDS)
146 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name);
147 #endif
148 return 0;
153 snd_seq_oss_synth_remove(struct device *_dev)
155 struct snd_seq_device *dev = to_seq_dev(_dev);
156 int index;
157 struct seq_oss_synth *rec = dev->driver_data;
158 unsigned long flags;
160 spin_lock_irqsave(&register_lock, flags);
161 for (index = 0; index < max_synth_devs; index++) {
162 if (synth_devs[index] == rec)
163 break;
165 if (index >= max_synth_devs) {
166 spin_unlock_irqrestore(&register_lock, flags);
167 pr_err("ALSA: seq_oss: can't unregister synth\n");
168 return -EINVAL;
170 synth_devs[index] = NULL;
171 if (index == max_synth_devs - 1) {
172 for (index--; index >= 0; index--) {
173 if (synth_devs[index])
174 break;
176 max_synth_devs = index + 1;
178 spin_unlock_irqrestore(&register_lock, flags);
179 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
180 if (rec->seq_device < SNDRV_CARDS)
181 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device);
182 #endif
184 snd_use_lock_sync(&rec->use_lock);
185 kfree(rec);
187 return 0;
193 static struct seq_oss_synth *
194 get_sdev(int dev)
196 struct seq_oss_synth *rec;
197 unsigned long flags;
199 spin_lock_irqsave(&register_lock, flags);
200 rec = synth_devs[dev];
201 if (rec)
202 snd_use_lock_use(&rec->use_lock);
203 spin_unlock_irqrestore(&register_lock, flags);
204 return rec;
209 * set up synth tables
212 void
213 snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
215 int i;
216 struct seq_oss_synth *rec;
217 struct seq_oss_synthinfo *info;
219 dp->max_synthdev = max_synth_devs;
220 dp->synth_opened = 0;
221 memset(dp->synths, 0, sizeof(dp->synths));
222 for (i = 0; i < dp->max_synthdev; i++) {
223 rec = get_sdev(i);
224 if (rec == NULL)
225 continue;
226 if (rec->oper.open == NULL || rec->oper.close == NULL) {
227 snd_use_lock_free(&rec->use_lock);
228 continue;
230 info = &dp->synths[i];
231 info->arg.app_index = dp->port;
232 info->arg.file_mode = dp->file_mode;
233 info->arg.seq_mode = dp->seq_mode;
234 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
235 info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
236 else
237 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
238 info->opened = 0;
239 if (!try_module_get(rec->oper.owner)) {
240 snd_use_lock_free(&rec->use_lock);
241 continue;
243 if (rec->oper.open(&info->arg, rec->private_data) < 0) {
244 module_put(rec->oper.owner);
245 snd_use_lock_free(&rec->use_lock);
246 continue;
248 info->nr_voices = rec->nr_voices;
249 if (info->nr_voices > 0) {
250 info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL);
251 if (!info->ch) {
252 rec->oper.close(&info->arg);
253 module_put(rec->oper.owner);
254 snd_use_lock_free(&rec->use_lock);
255 continue;
257 reset_channels(info);
259 info->opened++;
260 rec->opened++;
261 dp->synth_opened++;
262 snd_use_lock_free(&rec->use_lock);
268 * set up synth tables for MIDI emulation - /dev/music mode only
271 void
272 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp)
274 int i;
276 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
277 return;
279 for (i = 0; i < dp->max_mididev; i++) {
280 struct seq_oss_synthinfo *info;
281 info = &dp->synths[dp->max_synthdev];
282 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
283 continue;
284 info->arg.app_index = dp->port;
285 info->arg.file_mode = dp->file_mode;
286 info->arg.seq_mode = dp->seq_mode;
287 info->arg.private_data = info;
288 info->is_midi = 1;
289 info->midi_mapped = i;
290 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
291 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
292 info->opened = 1;
293 midi_synth_dev.opened++;
294 dp->max_synthdev++;
295 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
296 break;
302 * clean up synth tables
305 void
306 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp)
308 int i;
309 struct seq_oss_synth *rec;
310 struct seq_oss_synthinfo *info;
312 if (snd_BUG_ON(dp->max_synthdev > SNDRV_SEQ_OSS_MAX_SYNTH_DEVS))
313 return;
314 for (i = 0; i < dp->max_synthdev; i++) {
315 info = &dp->synths[i];
316 if (! info->opened)
317 continue;
318 if (info->is_midi) {
319 if (midi_synth_dev.opened > 0) {
320 snd_seq_oss_midi_close(dp, info->midi_mapped);
321 midi_synth_dev.opened--;
323 } else {
324 rec = get_sdev(i);
325 if (rec == NULL)
326 continue;
327 if (rec->opened > 0) {
328 rec->oper.close(&info->arg);
329 module_put(rec->oper.owner);
330 rec->opened = 0;
332 snd_use_lock_free(&rec->use_lock);
334 kfree(info->sysex);
335 info->sysex = NULL;
336 kfree(info->ch);
337 info->ch = NULL;
339 dp->synth_opened = 0;
340 dp->max_synthdev = 0;
343 static struct seq_oss_synthinfo *
344 get_synthinfo_nospec(struct seq_oss_devinfo *dp, int dev)
346 if (dev < 0 || dev >= dp->max_synthdev)
347 return NULL;
348 dev = array_index_nospec(dev, SNDRV_SEQ_OSS_MAX_SYNTH_DEVS);
349 return &dp->synths[dev];
353 * return synth device information pointer
355 static struct seq_oss_synth *
356 get_synthdev(struct seq_oss_devinfo *dp, int dev)
358 struct seq_oss_synth *rec;
359 struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev);
361 if (!info)
362 return NULL;
363 if (!info->opened)
364 return NULL;
365 if (info->is_midi) {
366 rec = &midi_synth_dev;
367 snd_use_lock_use(&rec->use_lock);
368 } else {
369 rec = get_sdev(dev);
370 if (!rec)
371 return NULL;
373 if (! rec->opened) {
374 snd_use_lock_free(&rec->use_lock);
375 return NULL;
377 return rec;
382 * reset note and velocity on each channel.
384 static void
385 reset_channels(struct seq_oss_synthinfo *info)
387 int i;
388 if (info->ch == NULL || ! info->nr_voices)
389 return;
390 for (i = 0; i < info->nr_voices; i++) {
391 info->ch[i].note = -1;
392 info->ch[i].vel = 0;
398 * reset synth device:
399 * call reset callback. if no callback is defined, send a heartbeat
400 * event to the corresponding port.
402 void
403 snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
405 struct seq_oss_synth *rec;
406 struct seq_oss_synthinfo *info;
408 info = get_synthinfo_nospec(dp, dev);
409 if (!info || !info->opened)
410 return;
411 if (info->sysex)
412 info->sysex->len = 0; /* reset sysex */
413 reset_channels(info);
414 if (info->is_midi) {
415 if (midi_synth_dev.opened <= 0)
416 return;
417 snd_seq_oss_midi_reset(dp, info->midi_mapped);
418 /* reopen the device */
419 snd_seq_oss_midi_close(dp, dev);
420 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
421 dp->file_mode) < 0) {
422 midi_synth_dev.opened--;
423 info->opened = 0;
424 kfree(info->sysex);
425 info->sysex = NULL;
426 kfree(info->ch);
427 info->ch = NULL;
429 return;
432 rec = get_sdev(dev);
433 if (rec == NULL)
434 return;
435 if (rec->oper.reset) {
436 rec->oper.reset(&info->arg);
437 } else {
438 struct snd_seq_event ev;
439 memset(&ev, 0, sizeof(ev));
440 snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client,
441 info->arg.addr.port);
442 ev.type = SNDRV_SEQ_EVENT_RESET;
443 snd_seq_oss_dispatch(dp, &ev, 0, 0);
445 snd_use_lock_free(&rec->use_lock);
450 * load a patch record:
451 * call load_patch callback function
454 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt,
455 const char __user *buf, int p, int c)
457 struct seq_oss_synth *rec;
458 struct seq_oss_synthinfo *info;
459 int rc;
461 info = get_synthinfo_nospec(dp, dev);
462 if (!info)
463 return -ENXIO;
465 if (info->is_midi)
466 return 0;
467 if ((rec = get_synthdev(dp, dev)) == NULL)
468 return -ENXIO;
470 if (rec->oper.load_patch == NULL)
471 rc = -ENXIO;
472 else
473 rc = rec->oper.load_patch(&info->arg, fmt, buf, p, c);
474 snd_use_lock_free(&rec->use_lock);
475 return rc;
479 * check if the device is valid synth device and return the synth info
481 struct seq_oss_synthinfo *
482 snd_seq_oss_synth_info(struct seq_oss_devinfo *dp, int dev)
484 struct seq_oss_synth *rec;
486 rec = get_synthdev(dp, dev);
487 if (rec) {
488 snd_use_lock_free(&rec->use_lock);
489 return get_synthinfo_nospec(dp, dev);
491 return NULL;
496 * receive OSS 6 byte sysex packet:
497 * the full sysex message will be sent if it reaches to the end of data
498 * (0xff).
501 snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
503 int i, send;
504 unsigned char *dest;
505 struct seq_oss_synth_sysex *sysex;
506 struct seq_oss_synthinfo *info;
508 info = snd_seq_oss_synth_info(dp, dev);
509 if (!info)
510 return -ENXIO;
512 sysex = info->sysex;
513 if (sysex == NULL) {
514 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
515 if (sysex == NULL)
516 return -ENOMEM;
517 info->sysex = sysex;
520 send = 0;
521 dest = sysex->buf + sysex->len;
522 /* copy 6 byte packet to the buffer */
523 for (i = 0; i < 6; i++) {
524 if (buf[i] == 0xff) {
525 send = 1;
526 break;
528 dest[i] = buf[i];
529 sysex->len++;
530 if (sysex->len >= MAX_SYSEX_BUFLEN) {
531 sysex->len = 0;
532 sysex->skip = 1;
533 break;
537 if (sysex->len && send) {
538 if (sysex->skip) {
539 sysex->skip = 0;
540 sysex->len = 0;
541 return -EINVAL; /* skip */
543 /* copy the data to event record and send it */
544 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
545 if (snd_seq_oss_synth_addr(dp, dev, ev))
546 return -EINVAL;
547 ev->data.ext.len = sysex->len;
548 ev->data.ext.ptr = sysex->buf;
549 sysex->len = 0;
550 return 0;
553 return -EINVAL; /* skip */
557 * fill the event source/destination addresses
560 snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
562 struct seq_oss_synthinfo *info = snd_seq_oss_synth_info(dp, dev);
564 if (!info)
565 return -EINVAL;
566 snd_seq_oss_fill_addr(dp, ev, info->arg.addr.client,
567 info->arg.addr.port);
568 return 0;
573 * OSS compatible ioctl
576 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
578 struct seq_oss_synth *rec;
579 struct seq_oss_synthinfo *info;
580 int rc;
582 info = get_synthinfo_nospec(dp, dev);
583 if (!info || info->is_midi)
584 return -ENXIO;
585 if ((rec = get_synthdev(dp, dev)) == NULL)
586 return -ENXIO;
587 if (rec->oper.ioctl == NULL)
588 rc = -ENXIO;
589 else
590 rc = rec->oper.ioctl(&info->arg, cmd, addr);
591 snd_use_lock_free(&rec->use_lock);
592 return rc;
597 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
600 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
602 struct seq_oss_synthinfo *info;
604 info = snd_seq_oss_synth_info(dp, dev);
605 if (!info || info->is_midi)
606 return -ENXIO;
607 ev->type = SNDRV_SEQ_EVENT_OSS;
608 memcpy(ev->data.raw8.d, data, 8);
609 return snd_seq_oss_synth_addr(dp, dev, ev);
614 * create OSS compatible synth_info record
617 snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
619 struct seq_oss_synth *rec;
621 if (dev < 0 || dev >= dp->max_synthdev)
622 return -ENXIO;
624 if (dp->synths[dev].is_midi) {
625 struct midi_info minf;
626 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
627 inf->synth_type = SYNTH_TYPE_MIDI;
628 inf->synth_subtype = 0;
629 inf->nr_voices = 16;
630 inf->device = dev;
631 strlcpy(inf->name, minf.name, sizeof(inf->name));
632 } else {
633 if ((rec = get_synthdev(dp, dev)) == NULL)
634 return -ENXIO;
635 inf->synth_type = rec->synth_type;
636 inf->synth_subtype = rec->synth_subtype;
637 inf->nr_voices = rec->nr_voices;
638 inf->device = dev;
639 strlcpy(inf->name, rec->name, sizeof(inf->name));
640 snd_use_lock_free(&rec->use_lock);
642 return 0;
646 #ifdef CONFIG_SND_PROC_FS
648 * proc interface
650 void
651 snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
653 int i;
654 struct seq_oss_synth *rec;
656 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
657 for (i = 0; i < max_synth_devs; i++) {
658 snd_iprintf(buf, "\nsynth %d: ", i);
659 rec = get_sdev(i);
660 if (rec == NULL) {
661 snd_iprintf(buf, "*empty*\n");
662 continue;
664 snd_iprintf(buf, "[%s]\n", rec->name);
665 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
666 rec->synth_type, rec->synth_subtype,
667 rec->nr_voices);
668 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
669 enabled_str((long)rec->oper.ioctl),
670 enabled_str((long)rec->oper.load_patch));
671 snd_use_lock_free(&rec->use_lock);
674 #endif /* CONFIG_SND_PROC_FS */