835edc80f9186fdcdb269fc455f540d8b3f68a76
[deliverable/linux.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 #include <linux/module.h>
28 #include <linux/slab.h>
29
30 /*
31 * constants
32 */
33 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
34 #define MAX_SYSEX_BUFLEN 128
35
36
37 /*
38 * definition of synth info records
39 */
40
41 /* sysex buffer */
42 struct seq_oss_synth_sysex {
43 int len;
44 int skip;
45 unsigned char buf[MAX_SYSEX_BUFLEN];
46 };
47
48 /* synth info */
49 struct seq_oss_synth {
50 int seq_device;
51
52 /* for synth_info */
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 * device table
69 */
70 static int max_synth_devs;
71 static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
72 static struct seq_oss_synth midi_synth_dev = {
73 -1, /* seq_device */
74 SYNTH_TYPE_MIDI, /* synth_type */
75 0, /* synth_subtype */
76 16, /* nr_voices */
77 "MIDI", /* name */
78 };
79
80 static DEFINE_SPINLOCK(register_lock);
81
82 /*
83 * prototypes
84 */
85 static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
86 static void reset_channels(struct seq_oss_synthinfo *info);
87
88 /*
89 * global initialization
90 */
91 void __init
92 snd_seq_oss_synth_init(void)
93 {
94 snd_use_lock_init(&midi_synth_dev.use_lock);
95 }
96
97 /*
98 * registration of the synth device
99 */
100 int
101 snd_seq_oss_synth_probe(struct device *_dev)
102 {
103 struct snd_seq_device *dev = to_seq_dev(_dev);
104 int i;
105 struct seq_oss_synth *rec;
106 struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
107 unsigned long flags;
108
109 if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL) {
110 pr_err("ALSA: seq_oss: can't malloc synth info\n");
111 return -ENOMEM;
112 }
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);
121
122 /* copy and truncate the name of synth device */
123 strlcpy(rec->name, dev->name, sizeof(rec->name));
124
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;
130 }
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;
137 }
138 max_synth_devs++;
139 }
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;
149 }
150
151
152 int
153 snd_seq_oss_synth_remove(struct device *_dev)
154 {
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;
159
160 spin_lock_irqsave(&register_lock, flags);
161 for (index = 0; index < max_synth_devs; index++) {
162 if (synth_devs[index] == rec)
163 break;
164 }
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;
169 }
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;
175 }
176 max_synth_devs = index + 1;
177 }
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
183
184 snd_use_lock_sync(&rec->use_lock);
185 kfree(rec);
186
187 return 0;
188 }
189
190
191 /*
192 */
193 static struct seq_oss_synth *
194 get_sdev(int dev)
195 {
196 struct seq_oss_synth *rec;
197 unsigned long flags;
198
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;
205 }
206
207
208 /*
209 * set up synth tables
210 */
211
212 void
213 snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
214 {
215 int i;
216 struct seq_oss_synth *rec;
217 struct seq_oss_synthinfo *info;
218
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;
229 }
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;
242 }
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;
247 }
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 pr_err("ALSA: seq_oss: Cannot malloc voices\n");
253 rec->oper.close(&info->arg);
254 module_put(rec->oper.owner);
255 snd_use_lock_free(&rec->use_lock);
256 continue;
257 }
258 reset_channels(info);
259 }
260 info->opened++;
261 rec->opened++;
262 dp->synth_opened++;
263 snd_use_lock_free(&rec->use_lock);
264 }
265 }
266
267
268 /*
269 * set up synth tables for MIDI emulation - /dev/music mode only
270 */
271
272 void
273 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp)
274 {
275 int i;
276
277 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
278 return;
279
280 for (i = 0; i < dp->max_mididev; i++) {
281 struct seq_oss_synthinfo *info;
282 info = &dp->synths[dp->max_synthdev];
283 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
284 continue;
285 info->arg.app_index = dp->port;
286 info->arg.file_mode = dp->file_mode;
287 info->arg.seq_mode = dp->seq_mode;
288 info->arg.private_data = info;
289 info->is_midi = 1;
290 info->midi_mapped = i;
291 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
292 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
293 info->opened = 1;
294 midi_synth_dev.opened++;
295 dp->max_synthdev++;
296 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
297 break;
298 }
299 }
300
301
302 /*
303 * clean up synth tables
304 */
305
306 void
307 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp)
308 {
309 int i;
310 struct seq_oss_synth *rec;
311 struct seq_oss_synthinfo *info;
312
313 if (snd_BUG_ON(dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS))
314 return;
315 for (i = 0; i < dp->max_synthdev; i++) {
316 info = &dp->synths[i];
317 if (! info->opened)
318 continue;
319 if (info->is_midi) {
320 if (midi_synth_dev.opened > 0) {
321 snd_seq_oss_midi_close(dp, info->midi_mapped);
322 midi_synth_dev.opened--;
323 }
324 } else {
325 rec = get_sdev(i);
326 if (rec == NULL)
327 continue;
328 if (rec->opened > 0) {
329 rec->oper.close(&info->arg);
330 module_put(rec->oper.owner);
331 rec->opened = 0;
332 }
333 snd_use_lock_free(&rec->use_lock);
334 }
335 kfree(info->sysex);
336 info->sysex = NULL;
337 kfree(info->ch);
338 info->ch = NULL;
339 }
340 dp->synth_opened = 0;
341 dp->max_synthdev = 0;
342 }
343
344 /*
345 * check if the specified device is MIDI mapped device
346 */
347 static int
348 is_midi_dev(struct seq_oss_devinfo *dp, int dev)
349 {
350 if (dev < 0 || dev >= dp->max_synthdev)
351 return 0;
352 if (dp->synths[dev].is_midi)
353 return 1;
354 return 0;
355 }
356
357 /*
358 * return synth device information pointer
359 */
360 static struct seq_oss_synth *
361 get_synthdev(struct seq_oss_devinfo *dp, int dev)
362 {
363 struct seq_oss_synth *rec;
364 if (dev < 0 || dev >= dp->max_synthdev)
365 return NULL;
366 if (! dp->synths[dev].opened)
367 return NULL;
368 if (dp->synths[dev].is_midi)
369 return &midi_synth_dev;
370 if ((rec = get_sdev(dev)) == NULL)
371 return NULL;
372 if (! rec->opened) {
373 snd_use_lock_free(&rec->use_lock);
374 return NULL;
375 }
376 return rec;
377 }
378
379
380 /*
381 * reset note and velocity on each channel.
382 */
383 static void
384 reset_channels(struct seq_oss_synthinfo *info)
385 {
386 int i;
387 if (info->ch == NULL || ! info->nr_voices)
388 return;
389 for (i = 0; i < info->nr_voices; i++) {
390 info->ch[i].note = -1;
391 info->ch[i].vel = 0;
392 }
393 }
394
395
396 /*
397 * reset synth device:
398 * call reset callback. if no callback is defined, send a heartbeat
399 * event to the corresponding port.
400 */
401 void
402 snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
403 {
404 struct seq_oss_synth *rec;
405 struct seq_oss_synthinfo *info;
406
407 if (snd_BUG_ON(dev < 0 || dev >= dp->max_synthdev))
408 return;
409 info = &dp->synths[dev];
410 if (! info->opened)
411 return;
412 if (info->sysex)
413 info->sysex->len = 0; /* reset sysex */
414 reset_channels(info);
415 if (info->is_midi) {
416 if (midi_synth_dev.opened <= 0)
417 return;
418 snd_seq_oss_midi_reset(dp, info->midi_mapped);
419 /* reopen the device */
420 snd_seq_oss_midi_close(dp, dev);
421 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
422 dp->file_mode) < 0) {
423 midi_synth_dev.opened--;
424 info->opened = 0;
425 kfree(info->sysex);
426 info->sysex = NULL;
427 kfree(info->ch);
428 info->ch = NULL;
429 }
430 return;
431 }
432
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 struct snd_seq_event 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);
445 }
446 snd_use_lock_free(&rec->use_lock);
447 }
448
449
450 /*
451 * load a patch record:
452 * call load_patch callback function
453 */
454 int
455 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt,
456 const char __user *buf, int p, int c)
457 {
458 struct seq_oss_synth *rec;
459 int rc;
460
461 if (dev < 0 || dev >= dp->max_synthdev)
462 return -ENXIO;
463
464 if (is_midi_dev(dp, dev))
465 return 0;
466 if ((rec = get_synthdev(dp, dev)) == NULL)
467 return -ENXIO;
468
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;
475 }
476
477 /*
478 * check if the device is valid synth device
479 */
480 int
481 snd_seq_oss_synth_is_valid(struct seq_oss_devinfo *dp, int dev)
482 {
483 struct seq_oss_synth *rec;
484 rec = get_synthdev(dp, dev);
485 if (rec) {
486 snd_use_lock_free(&rec->use_lock);
487 return 1;
488 }
489 return 0;
490 }
491
492
493 /*
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).
497 */
498 int
499 snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
500 {
501 int i, send;
502 unsigned char *dest;
503 struct seq_oss_synth_sysex *sysex;
504
505 if (! snd_seq_oss_synth_is_valid(dp, dev))
506 return -ENXIO;
507
508 sysex = dp->synths[dev].sysex;
509 if (sysex == NULL) {
510 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
511 if (sysex == NULL)
512 return -ENOMEM;
513 dp->synths[dev].sysex = sysex;
514 }
515
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;
523 }
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;
530 }
531 }
532
533 if (sysex->len && send) {
534 if (sysex->skip) {
535 sysex->skip = 0;
536 sysex->len = 0;
537 return -EINVAL; /* skip */
538 }
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;
547 }
548
549 return -EINVAL; /* skip */
550 }
551
552 /*
553 * fill the event source/destination addresses
554 */
555 int
556 snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
557 {
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;
563 }
564
565
566 /*
567 * OSS compatible ioctl
568 */
569 int
570 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
571 {
572 struct seq_oss_synth *rec;
573 int rc;
574
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;
585 }
586
587
588 /*
589 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
590 */
591 int
592 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
593 {
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);
599 }
600
601
602 /*
603 * create OSS compatible synth_info record
604 */
605 int
606 snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
607 {
608 struct seq_oss_synth *rec;
609
610 if (dev < 0 || dev >= dp->max_synthdev)
611 return -ENXIO;
612
613 if (dp->synths[dev].is_midi) {
614 struct midi_info minf;
615 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
616 inf->synth_type = SYNTH_TYPE_MIDI;
617 inf->synth_subtype = 0;
618 inf->nr_voices = 16;
619 inf->device = dev;
620 strlcpy(inf->name, minf.name, sizeof(inf->name));
621 } else {
622 if ((rec = get_synthdev(dp, dev)) == NULL)
623 return -ENXIO;
624 inf->synth_type = rec->synth_type;
625 inf->synth_subtype = rec->synth_subtype;
626 inf->nr_voices = rec->nr_voices;
627 inf->device = dev;
628 strlcpy(inf->name, rec->name, sizeof(inf->name));
629 snd_use_lock_free(&rec->use_lock);
630 }
631 return 0;
632 }
633
634
635 #ifdef CONFIG_PROC_FS
636 /*
637 * proc interface
638 */
639 void
640 snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
641 {
642 int i;
643 struct seq_oss_synth *rec;
644
645 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
646 for (i = 0; i < max_synth_devs; i++) {
647 snd_iprintf(buf, "\nsynth %d: ", i);
648 rec = get_sdev(i);
649 if (rec == NULL) {
650 snd_iprintf(buf, "*empty*\n");
651 continue;
652 }
653 snd_iprintf(buf, "[%s]\n", rec->name);
654 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
655 rec->synth_type, rec->synth_subtype,
656 rec->nr_voices);
657 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
658 enabled_str((long)rec->oper.ioctl),
659 enabled_str((long)rec->oper.load_patch));
660 snd_use_lock_free(&rec->use_lock);
661 }
662 }
663 #endif /* CONFIG_PROC_FS */
This page took 0.060685 seconds and 4 git commands to generate.