]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/core/seq/oss/seq_oss_synth.c
ALSA: Kill snd_assert() in sound/core/*
[linux-2.6-omap-h63xx.git] / sound / core / seq / oss / seq_oss_synth.c
1 /*
2  * OSS compatible sequencer driver
3  *
4  * synth 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 "seq_oss_synth.h"
24 #include "seq_oss_midi.h"
25 #include "../seq_lock.h"
26 #include <linux/init.h>
27
28 /*
29  * constants
30  */
31 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME    30
32 #define MAX_SYSEX_BUFLEN                128
33
34
35 /*
36  * definition of synth info records
37  */
38
39 /* sysex buffer */
40 struct seq_oss_synth_sysex {
41         int len;
42         int skip;
43         unsigned char buf[MAX_SYSEX_BUFLEN];
44 };
45
46 /* synth info */
47 struct seq_oss_synth {
48         int seq_device;
49
50         /* for synth_info */
51         int synth_type;
52         int synth_subtype;
53         int nr_voices;
54
55         char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
56         struct snd_seq_oss_callback oper;
57
58         int opened;
59
60         void *private_data;
61         snd_use_lock_t use_lock;
62 };
63
64
65 /*
66  * device table
67  */
68 static int max_synth_devs;
69 static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
70 static struct seq_oss_synth midi_synth_dev = {
71         -1, /* seq_device */
72         SYNTH_TYPE_MIDI, /* synth_type */
73         0, /* synth_subtype */
74         16, /* nr_voices */
75         "MIDI", /* name */
76 };
77
78 static DEFINE_SPINLOCK(register_lock);
79
80 /*
81  * prototypes
82  */
83 static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
84 static void reset_channels(struct seq_oss_synthinfo *info);
85
86 /*
87  * global initialization
88  */
89 void __init
90 snd_seq_oss_synth_init(void)
91 {
92         snd_use_lock_init(&midi_synth_dev.use_lock);
93 }
94
95 /*
96  * registration of the synth device
97  */
98 int
99 snd_seq_oss_synth_register(struct snd_seq_device *dev)
100 {
101         int i;
102         struct seq_oss_synth *rec;
103         struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
104         unsigned long flags;
105
106         if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL) {
107                 snd_printk(KERN_ERR "can't malloc synth info\n");
108                 return -ENOMEM;
109         }
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);
118
119         /* copy and truncate the name of synth device */
120         strlcpy(rec->name, dev->name, sizeof(rec->name));
121
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;
127         }
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;
134                 }
135                 max_synth_devs++;
136         }
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;
147 }
148
149
150 int
151 snd_seq_oss_synth_unregister(struct snd_seq_device *dev)
152 {
153         int index;
154         struct seq_oss_synth *rec = dev->driver_data;
155         unsigned long flags;
156
157         spin_lock_irqsave(&register_lock, flags);
158         for (index = 0; index < max_synth_devs; index++) {
159                 if (synth_devs[index] == rec)
160                         break;
161         }
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;
166         }
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;
172                 }
173                 max_synth_devs = index + 1;
174         }
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
180
181         snd_use_lock_sync(&rec->use_lock);
182         kfree(rec);
183
184         return 0;
185 }
186
187
188 /*
189  */
190 static struct seq_oss_synth *
191 get_sdev(int dev)
192 {
193         struct seq_oss_synth *rec;
194         unsigned long flags;
195
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;
202 }
203
204
205 /*
206  * set up synth tables
207  */
208
209 void
210 snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
211 {
212         int i;
213         struct seq_oss_synth *rec;
214         struct seq_oss_synthinfo *info;
215
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;
226                 }
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;
239                 }
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;
244                 }
245                 info->nr_voices = rec->nr_voices;
246                 if (info->nr_voices > 0) {
247                         info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL);
248                         if (!info->ch) {
249                                 snd_printk(KERN_ERR "Cannot malloc\n");
250                                 rec->oper.close(&info->arg);
251                                 module_put(rec->oper.owner);
252                                 snd_use_lock_free(&rec->use_lock);
253                                 continue;
254                         }
255                         reset_channels(info);
256                 }
257                 debug_printk(("synth %d assigned\n", i));
258                 info->opened++;
259                 rec->opened++;
260                 dp->synth_opened++;
261                 snd_use_lock_free(&rec->use_lock);
262         }
263 }
264
265
266 /*
267  * set up synth tables for MIDI emulation - /dev/music mode only
268  */
269
270 void
271 snd_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  * clean up synth tables
302  */
303
304 void
305 snd_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                                 debug_printk(("synth %d closed\n", i));
328                                 rec->oper.close(&info->arg);
329                                 module_put(rec->oper.owner);
330                                 rec->opened = 0;
331                         }
332                         snd_use_lock_free(&rec->use_lock);
333                 }
334                 kfree(info->sysex);
335                 info->sysex = NULL;
336                 kfree(info->ch);
337                 info->ch = NULL;
338         }
339         dp->synth_opened = 0;
340         dp->max_synthdev = 0;
341 }
342
343 /*
344  * check if the specified device is MIDI mapped device
345  */
346 static int
347 is_midi_dev(struct seq_oss_devinfo *dp, int dev)
348 {
349         if (dev < 0 || dev >= dp->max_synthdev)
350                 return 0;
351         if (dp->synths[dev].is_midi)
352                 return 1;
353         return 0;
354 }
355
356 /*
357  * return synth device information pointer
358  */
359 static struct seq_oss_synth *
360 get_synthdev(struct seq_oss_devinfo *dp, int dev)
361 {
362         struct seq_oss_synth *rec;
363         if (dev < 0 || dev >= dp->max_synthdev)
364                 return NULL;
365         if (! dp->synths[dev].opened)
366                 return NULL;
367         if (dp->synths[dev].is_midi)
368                 return &midi_synth_dev;
369         if ((rec = get_sdev(dev)) == NULL)
370                 return NULL;
371         if (! rec->opened) {
372                 snd_use_lock_free(&rec->use_lock);
373                 return NULL;
374         }
375         return rec;
376 }
377
378
379 /*
380  * reset note and velocity on each channel.
381  */
382 static void
383 reset_channels(struct seq_oss_synthinfo *info)
384 {
385         int i;
386         if (info->ch == NULL || ! info->nr_voices)
387                 return;
388         for (i = 0; i < info->nr_voices; i++) {
389                 info->ch[i].note = -1;
390                 info->ch[i].vel = 0;
391         }
392 }
393
394
395 /*
396  * reset synth device:
397  * call reset callback.  if no callback is defined, send a heartbeat
398  * event to the corresponding port.
399  */
400 void
401 snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
402 {
403         struct seq_oss_synth *rec;
404         struct seq_oss_synthinfo *info;
405
406         if (snd_BUG_ON(dev < 0 || dev >= dp->max_synthdev))
407                 return;
408         info = &dp->synths[dev];
409         if (! 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;
428                 }
429                 return;
430         }
431
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);
444         }
445         snd_use_lock_free(&rec->use_lock);
446 }
447
448
449 /*
450  * load a patch record:
451  * call load_patch callback function
452  */
453 int
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)
456 {
457         struct seq_oss_synth *rec;
458         int rc;
459
460         if (dev < 0 || dev >= dp->max_synthdev)
461                 return -ENXIO;
462
463         if (is_midi_dev(dp, dev))
464                 return 0;
465         if ((rec = get_synthdev(dp, dev)) == NULL)
466                 return -ENXIO;
467
468         if (rec->oper.load_patch == NULL)
469                 rc = -ENXIO;
470         else
471                 rc = rec->oper.load_patch(&dp->synths[dev].arg, fmt, buf, p, c);
472         snd_use_lock_free(&rec->use_lock);
473         return rc;
474 }
475
476 /*
477  * check if the device is valid synth device
478  */
479 int
480 snd_seq_oss_synth_is_valid(struct seq_oss_devinfo *dp, int dev)
481 {
482         struct seq_oss_synth *rec;
483         rec = get_synthdev(dp, dev);
484         if (rec) {
485                 snd_use_lock_free(&rec->use_lock);
486                 return 1;
487         }
488         return 0;
489 }
490
491
492 /*
493  * receive OSS 6 byte sysex packet:
494  * the full sysex message will be sent if it reaches to the end of data
495  * (0xff).
496  */
497 int
498 snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
499 {
500         int i, send;
501         unsigned char *dest;
502         struct seq_oss_synth_sysex *sysex;
503
504         if (! snd_seq_oss_synth_is_valid(dp, dev))
505                 return -ENXIO;
506
507         sysex = dp->synths[dev].sysex;
508         if (sysex == NULL) {
509                 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
510                 if (sysex == NULL)
511                         return -ENOMEM;
512                 dp->synths[dev].sysex = sysex;
513         }
514
515         send = 0;
516         dest = sysex->buf + sysex->len;
517         /* copy 6 byte packet to the buffer */
518         for (i = 0; i < 6; i++) {
519                 if (buf[i] == 0xff) {
520                         send = 1;
521                         break;
522                 }
523                 dest[i] = buf[i];
524                 sysex->len++;
525                 if (sysex->len >= MAX_SYSEX_BUFLEN) {
526                         sysex->len = 0;
527                         sysex->skip = 1;
528                         break;
529                 }
530         }
531
532         if (sysex->len && send) {
533                 if (sysex->skip) {
534                         sysex->skip = 0;
535                         sysex->len = 0;
536                         return -EINVAL; /* skip */
537                 }
538                 /* copy the data to event record and send it */
539                 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
540                 if (snd_seq_oss_synth_addr(dp, dev, ev))
541                         return -EINVAL;
542                 ev->data.ext.len = sysex->len;
543                 ev->data.ext.ptr = sysex->buf;
544                 sysex->len = 0;
545                 return 0;
546         }
547
548         return -EINVAL; /* skip */
549 }
550
551 /*
552  * fill the event source/destination addresses
553  */
554 int
555 snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
556 {
557         if (! snd_seq_oss_synth_is_valid(dp, dev))
558                 return -EINVAL;
559         snd_seq_oss_fill_addr(dp, ev, dp->synths[dev].arg.addr.client,
560                               dp->synths[dev].arg.addr.port);
561         return 0;
562 }
563
564
565 /*
566  * OSS compatible ioctl
567  */
568 int
569 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
570 {
571         struct seq_oss_synth *rec;
572         int rc;
573
574         if (is_midi_dev(dp, dev))
575                 return -ENXIO;
576         if ((rec = get_synthdev(dp, dev)) == NULL)
577                 return -ENXIO;
578         if (rec->oper.ioctl == NULL)
579                 rc = -ENXIO;
580         else
581                 rc = rec->oper.ioctl(&dp->synths[dev].arg, cmd, addr);
582         snd_use_lock_free(&rec->use_lock);
583         return rc;
584 }
585
586
587 /*
588  * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
589  */
590 int
591 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
592 {
593         if (! snd_seq_oss_synth_is_valid(dp, dev) || is_midi_dev(dp, dev))
594                 return -ENXIO;
595         ev->type = SNDRV_SEQ_EVENT_OSS;
596         memcpy(ev->data.raw8.d, data, 8);
597         return snd_seq_oss_synth_addr(dp, dev, ev);
598 }
599
600
601 /*
602  * create OSS compatible synth_info record
603  */
604 int
605 snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
606 {
607         struct seq_oss_synth *rec;
608
609         if (dev < 0 || dev >= dp->max_synthdev)
610                 return -ENXIO;
611
612         if (dp->synths[dev].is_midi) {
613                 struct midi_info minf;
614                 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
615                 inf->synth_type = SYNTH_TYPE_MIDI;
616                 inf->synth_subtype = 0;
617                 inf->nr_voices = 16;
618                 inf->device = dev;
619                 strlcpy(inf->name, minf.name, sizeof(inf->name));
620         } else {
621                 if ((rec = get_synthdev(dp, dev)) == NULL)
622                         return -ENXIO;
623                 inf->synth_type = rec->synth_type;
624                 inf->synth_subtype = rec->synth_subtype;
625                 inf->nr_voices = rec->nr_voices;
626                 inf->device = dev;
627                 strlcpy(inf->name, rec->name, sizeof(inf->name));
628                 snd_use_lock_free(&rec->use_lock);
629         }
630         return 0;
631 }
632
633
634 #ifdef CONFIG_PROC_FS
635 /*
636  * proc interface
637  */
638 void
639 snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
640 {
641         int i;
642         struct seq_oss_synth *rec;
643
644         snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
645         for (i = 0; i < max_synth_devs; i++) {
646                 snd_iprintf(buf, "\nsynth %d: ", i);
647                 rec = get_sdev(i);
648                 if (rec == NULL) {
649                         snd_iprintf(buf, "*empty*\n");
650                         continue;
651                 }
652                 snd_iprintf(buf, "[%s]\n", rec->name);
653                 snd_iprintf(buf, "  type 0x%x : subtype 0x%x : voices %d\n",
654                             rec->synth_type, rec->synth_subtype,
655                             rec->nr_voices);
656                 snd_iprintf(buf, "  capabilities : ioctl %s / load_patch %s\n",
657                             enabled_str((long)rec->oper.ioctl),
658                             enabled_str((long)rec->oper.load_patch));
659                 snd_use_lock_free(&rec->use_lock);
660         }
661 }
662 #endif /* CONFIG_PROC_FS */