ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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_t {
41         int len;
42         int skip;
43         unsigned char buf[MAX_SYSEX_BUFLEN];
44 };
45
46 /* synth info */
47 struct seq_oss_synth_t {
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         snd_seq_oss_callback_t 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 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 */
76 };
77
78 static spinlock_t register_lock = SPIN_LOCK_UNLOCKED;
79
80 /*
81  * prototypes
82  */
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);
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(snd_seq_device_t *dev)
100 {
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;
105
106         if ((rec = snd_kcalloc(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(snd_seq_device_t *dev)
152 {
153         int index;
154         seq_oss_synth_t *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 seq_oss_synth_t *
191 get_sdev(int dev)
192 {
193         seq_oss_synth_t *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(seq_oss_devinfo_t *dp)
211 {
212         int i;
213         seq_oss_synth_t *rec;
214         seq_oss_synthinfo_t *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 = snd_kcalloc(sizeof(seq_oss_chinfo_t) * info->nr_voices, GFP_KERNEL);
248                         reset_channels(info);
249                 }
250                 debug_printk(("synth %d assigned\n", i));
251                 info->opened++;
252                 rec->opened++;
253                 dp->synth_opened++;
254                 snd_use_lock_free(&rec->use_lock);
255         }
256 }
257
258
259 /*
260  * set up synth tables for MIDI emulation - /dev/music mode only
261  */
262
263 void
264 snd_seq_oss_synth_setup_midi(seq_oss_devinfo_t *dp)
265 {
266         int i;
267
268         if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
269                 return;
270
271         for (i = 0; i < dp->max_mididev; i++) {
272                 seq_oss_synthinfo_t *info;
273                 info = &dp->synths[dp->max_synthdev];
274                 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
275                         continue;
276                 info->arg.app_index = dp->port;
277                 info->arg.file_mode = dp->file_mode;
278                 info->arg.seq_mode = dp->seq_mode;
279                 info->arg.private_data = info;
280                 info->is_midi = 1;
281                 info->midi_mapped = i;
282                 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
283                 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
284                 info->opened = 1;
285                 midi_synth_dev.opened++;
286                 dp->max_synthdev++;
287                 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
288                         break;
289         }
290 }
291
292
293 /*
294  * clean up synth tables
295  */
296
297 void
298 snd_seq_oss_synth_cleanup(seq_oss_devinfo_t *dp)
299 {
300         int i;
301         seq_oss_synth_t *rec;
302         seq_oss_synthinfo_t *info;
303
304         snd_assert(dp->max_synthdev <= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS, return);
305         for (i = 0; i < dp->max_synthdev; i++) {
306                 info = &dp->synths[i];
307                 if (! info->opened)
308                         continue;
309                 if (info->is_midi) {
310                         if (midi_synth_dev.opened > 0) {
311                                 snd_seq_oss_midi_close(dp, info->midi_mapped);
312                                 midi_synth_dev.opened--;
313                         }
314                 } else {
315                         rec = get_sdev(i);
316                         if (rec == NULL)
317                                 continue;
318                         if (rec->opened > 0) {
319                                 debug_printk(("synth %d closed\n", i));
320                                 rec->oper.close(&info->arg);
321                                 module_put(rec->oper.owner);
322                                 rec->opened = 0;
323                         }
324                         snd_use_lock_free(&rec->use_lock);
325                 }
326                 if (info->sysex) {
327                         kfree(info->sysex);
328                         info->sysex = NULL;
329                 }
330                 if (info->ch) {
331                         kfree(info->ch);
332                         info->ch = NULL;
333                 }
334         }
335         dp->synth_opened = 0;
336         dp->max_synthdev = 0;
337 }
338
339 /*
340  * check if the specified device is MIDI mapped device
341  */
342 static int
343 is_midi_dev(seq_oss_devinfo_t *dp, int dev)
344 {
345         if (dev < 0 || dev >= dp->max_synthdev)
346                 return 0;
347         if (dp->synths[dev].is_midi)
348                 return 1;
349         return 0;
350 }
351
352 /*
353  * return synth device information pointer
354  */
355 static seq_oss_synth_t *
356 get_synthdev(seq_oss_devinfo_t *dp, int dev)
357 {
358         seq_oss_synth_t *rec;
359         if (dev < 0 || dev >= dp->max_synthdev)
360                 return NULL;
361         if (! dp->synths[dev].opened)
362                 return NULL;
363         if (dp->synths[dev].is_midi)
364                 return &midi_synth_dev;
365         if ((rec = get_sdev(dev)) == NULL)
366                 return NULL;
367         if (! rec->opened) {
368                 snd_use_lock_free(&rec->use_lock);
369                 return NULL;
370         }
371         return rec;
372 }
373
374
375 /*
376  * reset note and velocity on each channel.
377  */
378 static void
379 reset_channels(seq_oss_synthinfo_t *info)
380 {
381         int i;
382         if (info->ch == NULL || ! info->nr_voices)
383                 return;
384         for (i = 0; i < info->nr_voices; i++) {
385                 info->ch[i].note = -1;
386                 info->ch[i].vel = 0;
387         }
388 }
389
390
391 /*
392  * reset synth device:
393  * call reset callback.  if no callback is defined, send a heartbeat
394  * event to the corresponding port.
395  */
396 void
397 snd_seq_oss_synth_reset(seq_oss_devinfo_t *dp, int dev)
398 {
399         seq_oss_synth_t *rec;
400         seq_oss_synthinfo_t *info;
401
402         snd_assert(dev >= 0 && dev < dp->max_synthdev, return);
403         info = &dp->synths[dev];
404         if (! info->opened)
405                 return;
406         if (info->sysex)
407                 info->sysex->len = 0; /* reset sysex */
408         reset_channels(info);
409         if (info->is_midi) {
410                 if (midi_synth_dev.opened <= 0)
411                         return;
412                 snd_seq_oss_midi_reset(dp, info->midi_mapped);
413                 /* reopen the device */
414                 snd_seq_oss_midi_close(dp, dev);
415                 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
416                                           dp->file_mode) < 0) {
417                         midi_synth_dev.opened--;
418                         info->opened = 0;
419                         if (info->sysex) {
420                                 kfree(info->sysex);
421                                 info->sysex = NULL;
422                         }
423                         if (info->ch) {
424                                 kfree(info->ch);
425                                 info->ch = NULL;
426                         }
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                 snd_seq_event_t 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  * load a patch record:
450  * call load_patch callback function
451  */
452 int
453 snd_seq_oss_synth_load_patch(seq_oss_devinfo_t *dp, int dev, int fmt,
454                             const char __user *buf, int p, int c)
455 {
456         seq_oss_synth_t *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  * check if the device is valid synth device
477  */
478 int
479 snd_seq_oss_synth_is_valid(seq_oss_devinfo_t *dp, int dev)
480 {
481         seq_oss_synth_t *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  * receive OSS 6 byte sysex packet:
493  * the full sysex message will be sent if it reaches to the end of data
494  * (0xff).
495  */
496 int
497 snd_seq_oss_synth_sysex(seq_oss_devinfo_t *dp, int dev, unsigned char *buf, snd_seq_event_t *ev)
498 {
499         int i, send;
500         unsigned char *dest;
501         seq_oss_synth_sysex_t *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 = snd_kcalloc(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         /* copy 6 byte packet to the buffer */
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; /* skip */
536                 }
537                 /* copy the data to event record and send it */
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; /* skip */
548 }
549
550 /*
551  * fill the event source/destination addresses
552  */
553 int
554 snd_seq_oss_synth_addr(seq_oss_devinfo_t *dp, int dev, snd_seq_event_t *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  * OSS compatible ioctl
566  */
567 int
568 snd_seq_oss_synth_ioctl(seq_oss_devinfo_t *dp, int dev, unsigned int cmd, unsigned long addr)
569 {
570         seq_oss_synth_t *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  * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
588  */
589 int
590 snd_seq_oss_synth_raw_event(seq_oss_devinfo_t *dp, int dev, unsigned char *data, snd_seq_event_t *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  * create OSS compatible synth_info record
602  */
603 int
604 snd_seq_oss_synth_make_info(seq_oss_devinfo_t *dp, int dev, struct synth_info *inf)
605 {
606         seq_oss_synth_t *rec;
607
608         if (dp->synths[dev].is_midi) {
609                 struct midi_info minf;
610                 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
611                 inf->synth_type = SYNTH_TYPE_MIDI;
612                 inf->synth_subtype = 0;
613                 inf->nr_voices = 16;
614                 inf->device = dev;
615                 strlcpy(inf->name, minf.name, sizeof(inf->name));
616         } else {
617                 if ((rec = get_synthdev(dp, dev)) == NULL)
618                         return -ENXIO;
619                 inf->synth_type = rec->synth_type;
620                 inf->synth_subtype = rec->synth_subtype;
621                 inf->nr_voices = rec->nr_voices;
622                 inf->device = dev;
623                 strlcpy(inf->name, rec->name, sizeof(inf->name));
624                 snd_use_lock_free(&rec->use_lock);
625         }
626         return 0;
627 }
628
629
630 /*
631  * proc interface
632  */
633 void
634 snd_seq_oss_synth_info_read(snd_info_buffer_t *buf)
635 {
636         int i;
637         seq_oss_synth_t *rec;
638
639         snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
640         for (i = 0; i < max_synth_devs; i++) {
641                 snd_iprintf(buf, "\nsynth %d: ", i);
642                 rec = get_sdev(i);
643                 if (rec == NULL) {
644                         snd_iprintf(buf, "*empty*\n");
645                         continue;
646                 }
647                 snd_iprintf(buf, "[%s]\n", rec->name);
648                 snd_iprintf(buf, "  type 0x%x : subtype 0x%x : voices %d\n",
649                             rec->synth_type, rec->synth_subtype,
650                             rec->nr_voices);
651                 snd_iprintf(buf, "  capabilities : ioctl %s / load_patch %s\n",
652                             enabled_str((long)rec->oper.ioctl),
653                             enabled_str((long)rec->oper.load_patch));
654                 snd_use_lock_free(&rec->use_lock);
655         }
656 }
657