initial commit with v2.6.9
[linux-2.6.9-moxart.git] / sound / core / seq / oss / seq_oss_synth.c
blobdc44e665d2a26ca55646a3ac3f0e4dced863dea5
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>
29 * constants
31 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
32 #define MAX_SYSEX_BUFLEN 128
36 * definition of synth info records
39 /* sysex buffer */
40 struct seq_oss_synth_sysex_t {
41 int len;
42 int skip;
43 unsigned char buf[MAX_SYSEX_BUFLEN];
46 /* synth info */
47 struct seq_oss_synth_t {
48 int seq_device;
50 /* for synth_info */
51 int synth_type;
52 int synth_subtype;
53 int nr_voices;
55 char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
56 snd_seq_oss_callback_t oper;
58 int opened;
60 void *private_data;
61 snd_use_lock_t use_lock;
66 * device table
68 static int max_synth_devs;
69 static seq_oss_synth_t *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
70 static seq_oss_synth_t midi_synth_dev = {
71 -1, /* seq_device */
72 SYNTH_TYPE_MIDI, /* synth_type */
73 0, /* synth_subtype */
74 16, /* nr_voices */
75 "MIDI", /* name */
78 static spinlock_t register_lock = SPIN_LOCK_UNLOCKED;
81 * prototypes
83 static seq_oss_synth_t *get_synthdev(seq_oss_devinfo_t *dp, int dev);
84 static void reset_channels(seq_oss_synthinfo_t *info);
87 * global initialization
89 void __init
90 snd_seq_oss_synth_init(void)
92 snd_use_lock_init(&midi_synth_dev.use_lock);
96 * registration of the synth device
98 int
99 snd_seq_oss_synth_register(snd_seq_device_t *dev)
101 int i;
102 seq_oss_synth_t *rec;
103 snd_seq_oss_reg_t *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
104 unsigned long flags;
106 if ((rec = kcalloc(1, sizeof(*rec), GFP_KERNEL)) == NULL) {
107 snd_printk(KERN_ERR "can't malloc synth info\n");
108 return -ENOMEM;
110 rec->seq_device = -1;
111 rec->synth_type = reg->type;
112 rec->synth_subtype = reg->subtype;
113 rec->nr_voices = reg->nvoices;
114 rec->oper = reg->oper;
115 rec->private_data = reg->private_data;
116 rec->opened = 0;
117 snd_use_lock_init(&rec->use_lock);
119 /* copy and truncate the name of synth device */
120 strlcpy(rec->name, dev->name, sizeof(rec->name));
122 /* registration */
123 spin_lock_irqsave(&register_lock, flags);
124 for (i = 0; i < max_synth_devs; i++) {
125 if (synth_devs[i] == NULL)
126 break;
128 if (i >= max_synth_devs) {
129 if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) {
130 spin_unlock_irqrestore(&register_lock, flags);
131 snd_printk(KERN_ERR "no more synth slot\n");
132 kfree(rec);
133 return -ENOMEM;
135 max_synth_devs++;
137 rec->seq_device = i;
138 synth_devs[i] = rec;
139 debug_printk(("synth %s registered %d\n", rec->name, i));
140 spin_unlock_irqrestore(&register_lock, flags);
141 dev->driver_data = rec;
142 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
143 if (i < SNDRV_CARDS)
144 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name);
145 #endif
146 return 0;
151 snd_seq_oss_synth_unregister(snd_seq_device_t *dev)
153 int index;
154 seq_oss_synth_t *rec = dev->driver_data;
155 unsigned long flags;
157 spin_lock_irqsave(&register_lock, flags);
158 for (index = 0; index < max_synth_devs; index++) {
159 if (synth_devs[index] == rec)
160 break;
162 if (index >= max_synth_devs) {
163 spin_unlock_irqrestore(&register_lock, flags);
164 snd_printk(KERN_ERR "can't unregister synth\n");
165 return -EINVAL;
167 synth_devs[index] = NULL;
168 if (index == max_synth_devs - 1) {
169 for (index--; index >= 0; index--) {
170 if (synth_devs[index])
171 break;
173 max_synth_devs = index + 1;
175 spin_unlock_irqrestore(&register_lock, flags);
176 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
177 if (rec->seq_device < SNDRV_CARDS)
178 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device);
179 #endif
181 snd_use_lock_sync(&rec->use_lock);
182 kfree(rec);
184 return 0;
190 static seq_oss_synth_t *
191 get_sdev(int dev)
193 seq_oss_synth_t *rec;
194 unsigned long flags;
196 spin_lock_irqsave(&register_lock, flags);
197 rec = synth_devs[dev];
198 if (rec)
199 snd_use_lock_use(&rec->use_lock);
200 spin_unlock_irqrestore(&register_lock, flags);
201 return rec;
206 * set up synth tables
209 void
210 snd_seq_oss_synth_setup(seq_oss_devinfo_t *dp)
212 int i;
213 seq_oss_synth_t *rec;
214 seq_oss_synthinfo_t *info;
216 dp->max_synthdev = max_synth_devs;
217 dp->synth_opened = 0;
218 memset(dp->synths, 0, sizeof(dp->synths));
219 for (i = 0; i < dp->max_synthdev; i++) {
220 rec = get_sdev(i);
221 if (rec == NULL)
222 continue;
223 if (rec->oper.open == NULL || rec->oper.close == NULL) {
224 snd_use_lock_free(&rec->use_lock);
225 continue;
227 info = &dp->synths[i];
228 info->arg.app_index = dp->port;
229 info->arg.file_mode = dp->file_mode;
230 info->arg.seq_mode = dp->seq_mode;
231 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
232 info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
233 else
234 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
235 info->opened = 0;
236 if (!try_module_get(rec->oper.owner)) {
237 snd_use_lock_free(&rec->use_lock);
238 continue;
240 if (rec->oper.open(&info->arg, rec->private_data) < 0) {
241 module_put(rec->oper.owner);
242 snd_use_lock_free(&rec->use_lock);
243 continue;
245 info->nr_voices = rec->nr_voices;
246 if (info->nr_voices > 0) {
247 info->ch = kcalloc(info->nr_voices, sizeof(seq_oss_chinfo_t), GFP_KERNEL);
248 if (!info->ch)
249 BUG();
250 reset_channels(info);
252 debug_printk(("synth %d assigned\n", i));
253 info->opened++;
254 rec->opened++;
255 dp->synth_opened++;
256 snd_use_lock_free(&rec->use_lock);
262 * set up synth tables for MIDI emulation - /dev/music mode only
265 void
266 snd_seq_oss_synth_setup_midi(seq_oss_devinfo_t *dp)
268 int i;
270 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
271 return;
273 for (i = 0; i < dp->max_mididev; i++) {
274 seq_oss_synthinfo_t *info;
275 info = &dp->synths[dp->max_synthdev];
276 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
277 continue;
278 info->arg.app_index = dp->port;
279 info->arg.file_mode = dp->file_mode;
280 info->arg.seq_mode = dp->seq_mode;
281 info->arg.private_data = info;
282 info->is_midi = 1;
283 info->midi_mapped = i;
284 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
285 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
286 info->opened = 1;
287 midi_synth_dev.opened++;
288 dp->max_synthdev++;
289 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
290 break;
296 * clean up synth tables
299 void
300 snd_seq_oss_synth_cleanup(seq_oss_devinfo_t *dp)
302 int i;
303 seq_oss_synth_t *rec;
304 seq_oss_synthinfo_t *info;
306 snd_assert(dp->max_synthdev <= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS, return);
307 for (i = 0; i < dp->max_synthdev; i++) {
308 info = &dp->synths[i];
309 if (! info->opened)
310 continue;
311 if (info->is_midi) {
312 if (midi_synth_dev.opened > 0) {
313 snd_seq_oss_midi_close(dp, info->midi_mapped);
314 midi_synth_dev.opened--;
316 } else {
317 rec = get_sdev(i);
318 if (rec == NULL)
319 continue;
320 if (rec->opened > 0) {
321 debug_printk(("synth %d closed\n", i));
322 rec->oper.close(&info->arg);
323 module_put(rec->oper.owner);
324 rec->opened = 0;
326 snd_use_lock_free(&rec->use_lock);
328 if (info->sysex) {
329 kfree(info->sysex);
330 info->sysex = NULL;
332 if (info->ch) {
333 kfree(info->ch);
334 info->ch = NULL;
337 dp->synth_opened = 0;
338 dp->max_synthdev = 0;
342 * check if the specified device is MIDI mapped device
344 static int
345 is_midi_dev(seq_oss_devinfo_t *dp, int dev)
347 if (dev < 0 || dev >= dp->max_synthdev)
348 return 0;
349 if (dp->synths[dev].is_midi)
350 return 1;
351 return 0;
355 * return synth device information pointer
357 static seq_oss_synth_t *
358 get_synthdev(seq_oss_devinfo_t *dp, int dev)
360 seq_oss_synth_t *rec;
361 if (dev < 0 || dev >= dp->max_synthdev)
362 return NULL;
363 if (! dp->synths[dev].opened)
364 return NULL;
365 if (dp->synths[dev].is_midi)
366 return &midi_synth_dev;
367 if ((rec = get_sdev(dev)) == NULL)
368 return NULL;
369 if (! rec->opened) {
370 snd_use_lock_free(&rec->use_lock);
371 return NULL;
373 return rec;
378 * reset note and velocity on each channel.
380 static void
381 reset_channels(seq_oss_synthinfo_t *info)
383 int i;
384 if (info->ch == NULL || ! info->nr_voices)
385 return;
386 for (i = 0; i < info->nr_voices; i++) {
387 info->ch[i].note = -1;
388 info->ch[i].vel = 0;
394 * reset synth device:
395 * call reset callback. if no callback is defined, send a heartbeat
396 * event to the corresponding port.
398 void
399 snd_seq_oss_synth_reset(seq_oss_devinfo_t *dp, int dev)
401 seq_oss_synth_t *rec;
402 seq_oss_synthinfo_t *info;
404 snd_assert(dev >= 0 && dev < dp->max_synthdev, return);
405 info = &dp->synths[dev];
406 if (! info->opened)
407 return;
408 if (info->sysex)
409 info->sysex->len = 0; /* reset sysex */
410 reset_channels(info);
411 if (info->is_midi) {
412 if (midi_synth_dev.opened <= 0)
413 return;
414 snd_seq_oss_midi_reset(dp, info->midi_mapped);
415 /* reopen the device */
416 snd_seq_oss_midi_close(dp, dev);
417 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
418 dp->file_mode) < 0) {
419 midi_synth_dev.opened--;
420 info->opened = 0;
421 if (info->sysex) {
422 kfree(info->sysex);
423 info->sysex = NULL;
425 if (info->ch) {
426 kfree(info->ch);
427 info->ch = NULL;
430 return;
433 rec = get_sdev(dev);
434 if (rec == NULL)
435 return;
436 if (rec->oper.reset) {
437 rec->oper.reset(&info->arg);
438 } else {
439 snd_seq_event_t ev;
440 memset(&ev, 0, sizeof(ev));
441 snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client,
442 info->arg.addr.port);
443 ev.type = SNDRV_SEQ_EVENT_RESET;
444 snd_seq_oss_dispatch(dp, &ev, 0, 0);
446 snd_use_lock_free(&rec->use_lock);
451 * load a patch record:
452 * call load_patch callback function
455 snd_seq_oss_synth_load_patch(seq_oss_devinfo_t *dp, int dev, int fmt,
456 const char __user *buf, int p, int c)
458 seq_oss_synth_t *rec;
459 int rc;
461 if (dev < 0 || dev >= dp->max_synthdev)
462 return -ENXIO;
464 if (is_midi_dev(dp, dev))
465 return 0;
466 if ((rec = get_synthdev(dp, dev)) == NULL)
467 return -ENXIO;
469 if (rec->oper.load_patch == NULL)
470 rc = -ENXIO;
471 else
472 rc = rec->oper.load_patch(&dp->synths[dev].arg, fmt, buf, p, c);
473 snd_use_lock_free(&rec->use_lock);
474 return rc;
478 * check if the device is valid synth device
481 snd_seq_oss_synth_is_valid(seq_oss_devinfo_t *dp, int dev)
483 seq_oss_synth_t *rec;
484 rec = get_synthdev(dp, dev);
485 if (rec) {
486 snd_use_lock_free(&rec->use_lock);
487 return 1;
489 return 0;
494 * receive OSS 6 byte sysex packet:
495 * the full sysex message will be sent if it reaches to the end of data
496 * (0xff).
499 snd_seq_oss_synth_sysex(seq_oss_devinfo_t *dp, int dev, unsigned char *buf, snd_seq_event_t *ev)
501 int i, send;
502 unsigned char *dest;
503 seq_oss_synth_sysex_t *sysex;
505 if (! snd_seq_oss_synth_is_valid(dp, dev))
506 return -ENXIO;
508 sysex = dp->synths[dev].sysex;
509 if (sysex == NULL) {
510 sysex = kcalloc(1, sizeof(*sysex), GFP_KERNEL);
511 if (sysex == NULL)
512 return -ENOMEM;
513 dp->synths[dev].sysex = sysex;
516 send = 0;
517 dest = sysex->buf + sysex->len;
518 /* copy 6 byte packet to the buffer */
519 for (i = 0; i < 6; i++) {
520 if (buf[i] == 0xff) {
521 send = 1;
522 break;
524 dest[i] = buf[i];
525 sysex->len++;
526 if (sysex->len >= MAX_SYSEX_BUFLEN) {
527 sysex->len = 0;
528 sysex->skip = 1;
529 break;
533 if (sysex->len && send) {
534 if (sysex->skip) {
535 sysex->skip = 0;
536 sysex->len = 0;
537 return -EINVAL; /* skip */
539 /* copy the data to event record and send it */
540 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
541 if (snd_seq_oss_synth_addr(dp, dev, ev))
542 return -EINVAL;
543 ev->data.ext.len = sysex->len;
544 ev->data.ext.ptr = sysex->buf;
545 sysex->len = 0;
546 return 0;
549 return -EINVAL; /* skip */
553 * fill the event source/destination addresses
556 snd_seq_oss_synth_addr(seq_oss_devinfo_t *dp, int dev, snd_seq_event_t *ev)
558 if (! snd_seq_oss_synth_is_valid(dp, dev))
559 return -EINVAL;
560 snd_seq_oss_fill_addr(dp, ev, dp->synths[dev].arg.addr.client,
561 dp->synths[dev].arg.addr.port);
562 return 0;
567 * OSS compatible ioctl
570 snd_seq_oss_synth_ioctl(seq_oss_devinfo_t *dp, int dev, unsigned int cmd, unsigned long addr)
572 seq_oss_synth_t *rec;
573 int rc;
575 if (is_midi_dev(dp, dev))
576 return -ENXIO;
577 if ((rec = get_synthdev(dp, dev)) == NULL)
578 return -ENXIO;
579 if (rec->oper.ioctl == NULL)
580 rc = -ENXIO;
581 else
582 rc = rec->oper.ioctl(&dp->synths[dev].arg, cmd, addr);
583 snd_use_lock_free(&rec->use_lock);
584 return rc;
589 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
592 snd_seq_oss_synth_raw_event(seq_oss_devinfo_t *dp, int dev, unsigned char *data, snd_seq_event_t *ev)
594 if (! snd_seq_oss_synth_is_valid(dp, dev) || is_midi_dev(dp, dev))
595 return -ENXIO;
596 ev->type = SNDRV_SEQ_EVENT_OSS;
597 memcpy(ev->data.raw8.d, data, 8);
598 return snd_seq_oss_synth_addr(dp, dev, ev);
603 * create OSS compatible synth_info record
606 snd_seq_oss_synth_make_info(seq_oss_devinfo_t *dp, int dev, struct synth_info *inf)
608 seq_oss_synth_t *rec;
610 if (dp->synths[dev].is_midi) {
611 struct midi_info minf;
612 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
613 inf->synth_type = SYNTH_TYPE_MIDI;
614 inf->synth_subtype = 0;
615 inf->nr_voices = 16;
616 inf->device = dev;
617 strlcpy(inf->name, minf.name, sizeof(inf->name));
618 } else {
619 if ((rec = get_synthdev(dp, dev)) == NULL)
620 return -ENXIO;
621 inf->synth_type = rec->synth_type;
622 inf->synth_subtype = rec->synth_subtype;
623 inf->nr_voices = rec->nr_voices;
624 inf->device = dev;
625 strlcpy(inf->name, rec->name, sizeof(inf->name));
626 snd_use_lock_free(&rec->use_lock);
628 return 0;
633 * proc interface
635 void
636 snd_seq_oss_synth_info_read(snd_info_buffer_t *buf)
638 int i;
639 seq_oss_synth_t *rec;
641 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
642 for (i = 0; i < max_synth_devs; i++) {
643 snd_iprintf(buf, "\nsynth %d: ", i);
644 rec = get_sdev(i);
645 if (rec == NULL) {
646 snd_iprintf(buf, "*empty*\n");
647 continue;
649 snd_iprintf(buf, "[%s]\n", rec->name);
650 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
651 rec->synth_type, rec->synth_subtype,
652 rec->nr_voices);
653 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
654 enabled_str((long)rec->oper.ioctl),
655 enabled_str((long)rec->oper.load_patch));
656 snd_use_lock_free(&rec->use_lock);