blob: c0eea1dfe90ff5448cf1c6c43be2ebddbf4edf8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * sound/oss/sequencer.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * The sequencer personality manager.
5 */
6/*
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
12 */
13/*
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
15 * Alan Cox : reformatted and fixed a pair of null pointer bugs
16 */
17#include <linux/kmod.h>
18#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "sound_config.h"
20
21#include "midi_ctrl.h"
Arnd Bergmanncdef2e52014-01-02 13:07:51 +010022#include "sleep.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static int sequencer_ok;
25static struct sound_timer_operations *tmr;
26static int tmr_no = -1; /* Currently selected timer */
27static int pending_timer = -1; /* For timer change operation */
28extern unsigned long seq_time;
29
30static int obsolete_api_used;
31static DEFINE_SPINLOCK(lock);
32
33/*
34 * Local counts for number of synth and MIDI devices. These are initialized
35 * by the sequencer_open.
36 */
37static int max_mididev;
38static int max_synthdev;
39
40/*
41 * The seq_mode gives the operating mode of the sequencer:
42 * 1 = level1 (the default)
43 * 2 = level2 (extended capabilities)
44 */
45
46#define SEQ_1 1
47#define SEQ_2 2
48static int seq_mode = SEQ_1;
49
50static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper);
51static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper);
52
53static int midi_opened[MAX_MIDI_DEV];
54
55static int midi_written[MAX_MIDI_DEV];
56
57static unsigned long prev_input_time;
58static int prev_event_time;
59
60#include "tuning.h"
61
62#define EV_SZ 8
63#define IEV_SZ 8
64
65static unsigned char *queue;
66static unsigned char *iqueue;
67
68static volatile int qhead, qtail, qlen;
69static volatile int iqhead, iqtail, iqlen;
70static volatile int seq_playing;
71static volatile int sequencer_busy;
72static int output_threshold;
73static long pre_event_timeout;
74static unsigned synth_open_mask;
75
76static int seq_queue(unsigned char *note, char nonblock);
77static void seq_startplay(void);
78static int seq_sync(void);
79static void seq_reset(void);
80
81#if MAX_SYNTH_DEV > 15
82#error Too many synthesizer devices enabled.
83#endif
84
85int sequencer_read(int dev, struct file *file, char __user *buf, int count)
86{
87 int c = count, p = 0;
88 int ev_len;
89 unsigned long flags;
90
91 dev = dev >> 4;
92
93 ev_len = seq_mode == SEQ_1 ? 4 : 8;
94
95 spin_lock_irqsave(&lock,flags);
96
97 if (!iqlen)
98 {
99 spin_unlock_irqrestore(&lock,flags);
100 if (file->f_flags & O_NONBLOCK) {
101 return -EAGAIN;
102 }
103
Arnd Bergmanncdef2e52014-01-02 13:07:51 +0100104 oss_broken_sleep_on(&midi_sleeper, pre_event_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 spin_lock_irqsave(&lock,flags);
106 if (!iqlen)
107 {
108 spin_unlock_irqrestore(&lock,flags);
109 return 0;
110 }
111 }
112 while (iqlen && c >= ev_len)
113 {
114 char *fixit = (char *) &iqueue[iqhead * IEV_SZ];
115 spin_unlock_irqrestore(&lock,flags);
116 if (copy_to_user(&(buf)[p], fixit, ev_len))
117 return count - c;
118 p += ev_len;
119 c -= ev_len;
120
121 spin_lock_irqsave(&lock,flags);
122 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
123 iqlen--;
124 }
125 spin_unlock_irqrestore(&lock,flags);
126 return count - c;
127}
128
129static void sequencer_midi_output(int dev)
130{
131 /*
132 * Currently NOP
133 */
134}
135
136void seq_copy_to_input(unsigned char *event_rec, int len)
137{
138 unsigned long flags;
139
140 /*
141 * Verify that the len is valid for the current mode.
142 */
143
144 if (len != 4 && len != 8)
145 return;
146 if ((seq_mode == SEQ_1) != (len == 4))
147 return;
148
149 if (iqlen >= (SEQ_MAX_QUEUE - 1))
150 return; /* Overflow */
151
152 spin_lock_irqsave(&lock,flags);
153 memcpy(&iqueue[iqtail * IEV_SZ], event_rec, len);
154 iqlen++;
155 iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
156 wake_up(&midi_sleeper);
157 spin_unlock_irqrestore(&lock,flags);
158}
Adrian Bunkece7f772006-10-04 02:17:31 -0700159EXPORT_SYMBOL(seq_copy_to_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161static void sequencer_midi_input(int dev, unsigned char data)
162{
163 unsigned int tstamp;
164 unsigned char event_rec[4];
165
166 if (data == 0xfe) /* Ignore active sensing */
167 return;
168
169 tstamp = jiffies - seq_time;
170
171 if (tstamp != prev_input_time)
172 {
173 tstamp = (tstamp << 8) | SEQ_WAIT;
174 seq_copy_to_input((unsigned char *) &tstamp, 4);
175 prev_input_time = tstamp;
176 }
177 event_rec[0] = SEQ_MIDIPUTC;
178 event_rec[1] = data;
179 event_rec[2] = dev;
180 event_rec[3] = 0;
181
182 seq_copy_to_input(event_rec, 4);
183}
184
185void seq_input_event(unsigned char *event_rec, int len)
186{
187 unsigned long this_time;
188
189 if (seq_mode == SEQ_2)
190 this_time = tmr->get_time(tmr_no);
191 else
192 this_time = jiffies - seq_time;
193
194 if (this_time != prev_input_time)
195 {
196 unsigned char tmp_event[8];
197
198 tmp_event[0] = EV_TIMING;
199 tmp_event[1] = TMR_WAIT_ABS;
200 tmp_event[2] = 0;
201 tmp_event[3] = 0;
202 *(unsigned int *) &tmp_event[4] = this_time;
203
204 seq_copy_to_input(tmp_event, 8);
205 prev_input_time = this_time;
206 }
207 seq_copy_to_input(event_rec, len);
208}
Adrian Bunkece7f772006-10-04 02:17:31 -0700209EXPORT_SYMBOL(seq_input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211int sequencer_write(int dev, struct file *file, const char __user *buf, int count)
212{
213 unsigned char event_rec[EV_SZ], ev_code;
214 int p = 0, c, ev_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int mode = translate_mode(file);
216
217 dev = dev >> 4;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (mode == OPEN_READ)
220 return -EIO;
221
222 c = count;
223
224 while (c >= 4)
225 {
226 if (copy_from_user((char *) event_rec, &(buf)[p], 4))
227 goto out;
228 ev_code = event_rec[0];
229
230 if (ev_code == SEQ_FULLSIZE)
231 {
232 int err, fmt;
233
234 dev = *(unsigned short *) &event_rec[2];
235 if (dev < 0 || dev >= max_synthdev || synth_devs[dev] == NULL)
236 return -ENXIO;
237
238 if (!(synth_open_mask & (1 << dev)))
239 return -ENXIO;
240
241 fmt = (*(short *) &event_rec[0]) & 0xffff;
Dan Rosenbergb769f492011-03-23 10:53:41 -0400242 err = synth_devs[dev]->load_patch(dev, fmt, buf + p, c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (err < 0)
244 return err;
245
246 return err;
247 }
248 if (ev_code >= 128)
249 {
250 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
251 {
252 printk(KERN_WARNING "Sequencer: Invalid level 2 event %x\n", ev_code);
253 return -EINVAL;
254 }
255 ev_size = 8;
256
257 if (c < ev_size)
258 {
259 if (!seq_playing)
260 seq_startplay();
261 return count - c;
262 }
263 if (copy_from_user((char *)&event_rec[4],
264 &(buf)[p + 4], 4))
265 goto out;
266
267 }
268 else
269 {
270 if (seq_mode == SEQ_2)
271 {
272 printk(KERN_WARNING "Sequencer: 4 byte event in level 2 mode\n");
273 return -EINVAL;
274 }
275 ev_size = 4;
276
277 if (event_rec[0] != SEQ_MIDIPUTC)
278 obsolete_api_used = 1;
279 }
280
281 if (event_rec[0] == SEQ_MIDIPUTC)
282 {
283 if (!midi_opened[event_rec[2]])
284 {
Hannes Edere5bf4842009-02-25 22:29:47 +0100285 int err, mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int dev = event_rec[2];
287
288 if (dev >= max_mididev || midi_devs[dev]==NULL)
289 {
290 /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
291 return -ENXIO;
292 }
293 mode = translate_mode(file);
294
295 if ((err = midi_devs[dev]->open(dev, mode,
296 sequencer_midi_input, sequencer_midi_output)) < 0)
297 {
298 seq_reset();
299 printk(KERN_WARNING "Sequencer Error: Unable to open Midi #%d\n", dev);
300 return err;
301 }
302 midi_opened[dev] = 1;
303 }
304 }
305 if (!seq_queue(event_rec, (file->f_flags & (O_NONBLOCK) ? 1 : 0)))
306 {
307 int processed = count - c;
308
309 if (!seq_playing)
310 seq_startplay();
311
312 if (!processed && (file->f_flags & O_NONBLOCK))
313 return -EAGAIN;
314 else
315 return processed;
316 }
317 p += ev_size;
318 c -= ev_size;
319 }
320
321 if (!seq_playing)
322 seq_startplay();
323out:
324 return count;
325}
326
327static int seq_queue(unsigned char *note, char nonblock)
328{
329
330 /*
331 * Test if there is space in the queue
332 */
333
334 if (qlen >= SEQ_MAX_QUEUE)
335 if (!seq_playing)
336 seq_startplay(); /*
337 * Give chance to drain the queue
338 */
339
340 if (!nonblock && qlen >= SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
341 /*
342 * Sleep until there is enough space on the queue
343 */
Arnd Bergmanncdef2e52014-01-02 13:07:51 +0100344 oss_broken_sleep_on(&seq_sleeper, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 if (qlen >= SEQ_MAX_QUEUE)
347 {
348 return 0; /*
349 * To be sure
350 */
351 }
352 memcpy(&queue[qtail * EV_SZ], note, EV_SZ);
353
354 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
355 qlen++;
356
357 return 1;
358}
359
360static int extended_event(unsigned char *q)
361{
362 int dev = q[2];
363
364 if (dev < 0 || dev >= max_synthdev)
365 return -ENXIO;
366
367 if (!(synth_open_mask & (1 << dev)))
368 return -ENXIO;
369
370 switch (q[1])
371 {
372 case SEQ_NOTEOFF:
373 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
374 break;
375
376 case SEQ_NOTEON:
377 if (q[4] > 127 && q[4] != 255)
378 return 0;
379
380 if (q[5] == 0)
381 {
382 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
383 break;
384 }
385 synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
386 break;
387
388 case SEQ_PGMCHANGE:
389 synth_devs[dev]->set_instr(dev, q[3], q[4]);
390 break;
391
392 case SEQ_AFTERTOUCH:
393 synth_devs[dev]->aftertouch(dev, q[3], q[4]);
394 break;
395
396 case SEQ_BALANCE:
397 synth_devs[dev]->panning(dev, q[3], (char) q[4]);
398 break;
399
400 case SEQ_CONTROLLER:
401 synth_devs[dev]->controller(dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
402 break;
403
404 case SEQ_VOLMODE:
405 if (synth_devs[dev]->volume_method != NULL)
406 synth_devs[dev]->volume_method(dev, q[3]);
407 break;
408
409 default:
410 return -EINVAL;
411 }
412 return 0;
413}
414
415static int find_voice(int dev, int chn, int note)
416{
417 unsigned short key;
418 int i;
419
420 key = (chn << 8) | (note + 1);
421 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
422 if (synth_devs[dev]->alloc.map[i] == key)
423 return i;
424 return -1;
425}
426
427static int alloc_voice(int dev, int chn, int note)
428{
429 unsigned short key;
430 int voice;
431
432 key = (chn << 8) | (note + 1);
433
434 voice = synth_devs[dev]->alloc_voice(dev, chn, note,
435 &synth_devs[dev]->alloc);
436 synth_devs[dev]->alloc.map[voice] = key;
437 synth_devs[dev]->alloc.alloc_times[voice] =
438 synth_devs[dev]->alloc.timestamp++;
439 return voice;
440}
441
442static void seq_chn_voice_event(unsigned char *event_rec)
443{
444#define dev event_rec[1]
445#define cmd event_rec[2]
446#define chn event_rec[3]
447#define note event_rec[4]
448#define parm event_rec[5]
449
450 int voice = -1;
451
452 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
453 return;
454 if (!(synth_open_mask & (1 << dev)))
455 return;
456 if (!synth_devs[dev])
457 return;
458
459 if (seq_mode == SEQ_2)
460 {
461 if (synth_devs[dev]->alloc_voice)
462 voice = find_voice(dev, chn, note);
463
464 if (cmd == MIDI_NOTEON && parm == 0)
465 {
466 cmd = MIDI_NOTEOFF;
467 parm = 64;
468 }
469 }
470
471 switch (cmd)
472 {
473 case MIDI_NOTEON:
474 if (note > 127 && note != 255) /* Not a seq2 feature */
475 return;
476
477 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
478 {
479 /* Internal synthesizer (FM, GUS, etc) */
480 voice = alloc_voice(dev, chn, note);
481 }
482 if (voice == -1)
483 voice = chn;
484
485 if (seq_mode == SEQ_2 && (int) dev < num_synths)
486 {
487 /*
488 * The MIDI channel 10 is a percussive channel. Use the note
489 * number to select the proper patch (128 to 255) to play.
490 */
491
492 if (chn == 9)
493 {
494 synth_devs[dev]->set_instr(dev, voice, 128 + note);
495 synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
496 }
497 synth_devs[dev]->setup_voice(dev, voice, chn);
498 }
499 synth_devs[dev]->start_note(dev, voice, note, parm);
500 break;
501
502 case MIDI_NOTEOFF:
503 if (voice == -1)
504 voice = chn;
505 synth_devs[dev]->kill_note(dev, voice, note, parm);
506 break;
507
508 case MIDI_KEY_PRESSURE:
509 if (voice == -1)
510 voice = chn;
511 synth_devs[dev]->aftertouch(dev, voice, parm);
512 break;
513
514 default:;
515 }
516#undef dev
517#undef cmd
518#undef chn
519#undef note
520#undef parm
521}
522
523
524static void seq_chn_common_event(unsigned char *event_rec)
525{
526 unsigned char dev = event_rec[1];
527 unsigned char cmd = event_rec[2];
528 unsigned char chn = event_rec[3];
529 unsigned char p1 = event_rec[4];
530
531 /* unsigned char p2 = event_rec[5]; */
532 unsigned short w14 = *(short *) &event_rec[6];
533
534 if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
535 return;
536 if (!(synth_open_mask & (1 << dev)))
537 return;
538 if (!synth_devs[dev])
539 return;
540
541 switch (cmd)
542 {
543 case MIDI_PGM_CHANGE:
544 if (seq_mode == SEQ_2)
545 {
Dan Carpenter57220bc2013-03-15 09:14:22 +0300546 if (chn > 15)
547 break;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 synth_devs[dev]->chn_info[chn].pgm_num = p1;
550 if ((int) dev >= num_synths)
551 synth_devs[dev]->set_instr(dev, chn, p1);
552 }
553 else
554 synth_devs[dev]->set_instr(dev, chn, p1);
555
556 break;
557
558 case MIDI_CTL_CHANGE:
559 if (seq_mode == SEQ_2)
560 {
561 if (chn > 15 || p1 > 127)
562 break;
563
564 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
565
566 if (p1 < 32) /* Setting MSB should clear LSB to 0 */
567 synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
568
569 if ((int) dev < num_synths)
570 {
571 int val = w14 & 0x7f;
572 int i, key;
573
574 if (p1 < 64) /* Combine MSB and LSB */
575 {
576 val = ((synth_devs[dev]->
577 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
578 | (synth_devs[dev]->
579 chn_info[chn].controllers[p1 | 32] & 0x7f);
580 p1 &= ~32;
581 }
582 /* Handle all playing notes on this channel */
583
584 key = ((int) chn << 8);
585
586 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
587 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
588 synth_devs[dev]->controller(dev, i, p1, val);
589 }
590 else
591 synth_devs[dev]->controller(dev, chn, p1, w14);
592 }
593 else /* Mode 1 */
594 synth_devs[dev]->controller(dev, chn, p1, w14);
595 break;
596
597 case MIDI_PITCH_BEND:
598 if (seq_mode == SEQ_2)
599 {
Dan Carpenter57220bc2013-03-15 09:14:22 +0300600 if (chn > 15)
601 break;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 synth_devs[dev]->chn_info[chn].bender_value = w14;
604
605 if ((int) dev < num_synths)
606 {
607 /* Handle all playing notes on this channel */
608 int i, key;
609
610 key = (chn << 8);
611
612 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
613 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
614 synth_devs[dev]->bender(dev, i, w14);
615 }
616 else
617 synth_devs[dev]->bender(dev, chn, w14);
618 }
619 else /* MODE 1 */
620 synth_devs[dev]->bender(dev, chn, w14);
621 break;
622
623 default:;
624 }
625}
626
627static int seq_timing_event(unsigned char *event_rec)
628{
629 unsigned char cmd = event_rec[1];
630 unsigned int parm = *(int *) &event_rec[4];
631
632 if (seq_mode == SEQ_2)
633 {
634 int ret;
635
636 if ((ret = tmr->event(tmr_no, event_rec)) == TIMER_ARMED)
637 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
638 wake_up(&seq_sleeper);
639 return ret;
640 }
641 switch (cmd)
642 {
643 case TMR_WAIT_REL:
644 parm += prev_event_time;
645
646 /*
647 * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
648 * next case (TMR_WAIT_ABS)
649 */
650
651 case TMR_WAIT_ABS:
652 if (parm > 0)
653 {
654 long time;
655
656 time = parm;
657 prev_event_time = time;
658
659 seq_playing = 1;
660 request_sound_timer(time);
661
662 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
663 wake_up(&seq_sleeper);
664 return TIMER_ARMED;
665 }
666 break;
667
668 case TMR_START:
669 seq_time = jiffies;
670 prev_input_time = 0;
671 prev_event_time = 0;
672 break;
673
674 case TMR_STOP:
675 break;
676
677 case TMR_CONTINUE:
678 break;
679
680 case TMR_TEMPO:
681 break;
682
683 case TMR_ECHO:
684 if (seq_mode == SEQ_2)
685 seq_copy_to_input(event_rec, 8);
686 else
687 {
688 parm = (parm << 8 | SEQ_ECHO);
689 seq_copy_to_input((unsigned char *) &parm, 4);
690 }
691 break;
692
693 default:;
694 }
695
696 return TIMER_NOT_ARMED;
697}
698
699static void seq_local_event(unsigned char *event_rec)
700{
701 unsigned char cmd = event_rec[1];
702 unsigned int parm = *((unsigned int *) &event_rec[4]);
703
704 switch (cmd)
705 {
706 case LOCL_STARTAUDIO:
707 DMAbuf_start_devices(parm);
708 break;
709
710 default:;
711 }
712}
713
714static void seq_sysex_message(unsigned char *event_rec)
715{
Eugene Teo37f1e982006-03-25 03:08:25 -0800716 unsigned int dev = event_rec[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int i, l = 0;
718 unsigned char *buf = &event_rec[2];
719
Eugene Teo37f1e982006-03-25 03:08:25 -0800720 if (dev > max_synthdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return;
722 if (!(synth_open_mask & (1 << dev)))
723 return;
724 if (!synth_devs[dev])
725 return;
726
727 l = 0;
728 for (i = 0; i < 6 && buf[i] != 0xff; i++)
729 l = i + 1;
730
731 if (!synth_devs[dev]->send_sysex)
732 return;
733 if (l > 0)
734 synth_devs[dev]->send_sysex(dev, buf, l);
735}
736
737static int play_event(unsigned char *q)
738{
739 /*
740 * NOTE! This routine returns
741 * 0 = normal event played.
742 * 1 = Timer armed. Suspend playback until timer callback.
743 * 2 = MIDI output buffer full. Restore queue and suspend until timer
744 */
745 unsigned int *delay;
746
747 switch (q[0])
748 {
749 case SEQ_NOTEOFF:
750 if (synth_open_mask & (1 << 0))
751 if (synth_devs[0])
752 synth_devs[0]->kill_note(0, q[1], 255, q[3]);
753 break;
754
755 case SEQ_NOTEON:
756 if (q[4] < 128 || q[4] == 255)
757 if (synth_open_mask & (1 << 0))
758 if (synth_devs[0])
759 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
760 break;
761
762 case SEQ_WAIT:
763 delay = (unsigned int *) q; /*
764 * Bytes 1 to 3 are containing the *
765 * delay in 'ticks'
766 */
767 *delay = (*delay >> 8) & 0xffffff;
768
769 if (*delay > 0)
770 {
771 long time;
772
773 seq_playing = 1;
774 time = *delay;
775 prev_event_time = time;
776
777 request_sound_timer(time);
778
779 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
780 wake_up(&seq_sleeper);
781 /*
782 * The timer is now active and will reinvoke this function
783 * after the timer expires. Return to the caller now.
784 */
785 return 1;
786 }
787 break;
788
789 case SEQ_PGMCHANGE:
790 if (synth_open_mask & (1 << 0))
791 if (synth_devs[0])
792 synth_devs[0]->set_instr(0, q[1], q[2]);
793 break;
794
795 case SEQ_SYNCTIMER: /*
796 * Reset timer
797 */
798 seq_time = jiffies;
799 prev_input_time = 0;
800 prev_event_time = 0;
801 break;
802
803 case SEQ_MIDIPUTC: /*
804 * Put a midi character
805 */
806 if (midi_opened[q[2]])
807 {
808 int dev;
809
810 dev = q[2];
811
812 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
813 break;
814
815 if (!midi_devs[dev]->outputc(dev, q[1]))
816 {
817 /*
818 * Output FIFO is full. Wait one timer cycle and try again.
819 */
820
821 seq_playing = 1;
822 request_sound_timer(-1);
823 return 2;
824 }
825 else
826 midi_written[dev] = 1;
827 }
828 break;
829
830 case SEQ_ECHO:
831 seq_copy_to_input(q, 4); /*
832 * Echo back to the process
833 */
834 break;
835
836 case SEQ_PRIVATE:
837 if ((int) q[1] < max_synthdev)
838 synth_devs[q[1]]->hw_control(q[1], q);
839 break;
840
841 case SEQ_EXTENDED:
842 extended_event(q);
843 break;
844
845 case EV_CHN_VOICE:
846 seq_chn_voice_event(q);
847 break;
848
849 case EV_CHN_COMMON:
850 seq_chn_common_event(q);
851 break;
852
853 case EV_TIMING:
854 if (seq_timing_event(q) == TIMER_ARMED)
855 {
856 return 1;
857 }
858 break;
859
860 case EV_SEQ_LOCAL:
861 seq_local_event(q);
862 break;
863
864 case EV_SYSEX:
865 seq_sysex_message(q);
866 break;
867
868 default:;
869 }
870 return 0;
871}
872
873/* called also as timer in irq context */
874static void seq_startplay(void)
875{
876 int this_one, action;
877 unsigned long flags;
878
879 while (qlen > 0)
880 {
881
882 spin_lock_irqsave(&lock,flags);
883 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
884 qlen--;
885 spin_unlock_irqrestore(&lock,flags);
886
887 seq_playing = 1;
888
889 if ((action = play_event(&queue[this_one * EV_SZ])))
890 { /* Suspend playback. Next timer routine invokes this routine again */
891 if (action == 2)
892 {
893 qlen++;
894 qhead = this_one;
895 }
896 return;
897 }
898 }
899
900 seq_playing = 0;
901
902 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
903 wake_up(&seq_sleeper);
904}
905
906static void reset_controllers(int dev, unsigned char *controller, int update_dev)
907{
908 int i;
909 for (i = 0; i < 128; i++)
910 controller[i] = ctrl_def_values[i];
911}
912
913static void setup_mode2(void)
914{
915 int dev;
916
917 max_synthdev = num_synths;
918
919 for (dev = 0; dev < num_midis; dev++)
920 {
921 if (midi_devs[dev] && midi_devs[dev]->converter != NULL)
922 {
923 synth_devs[max_synthdev++] = midi_devs[dev]->converter;
924 }
925 }
926
927 for (dev = 0; dev < max_synthdev; dev++)
928 {
929 int chn;
930
931 synth_devs[dev]->sysex_ptr = 0;
932 synth_devs[dev]->emulation = 0;
933
934 for (chn = 0; chn < 16; chn++)
935 {
936 synth_devs[dev]->chn_info[chn].pgm_num = 0;
937 reset_controllers(dev,
938 synth_devs[dev]->chn_info[chn].controllers,0);
939 synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */
940 synth_devs[dev]->chn_info[chn].bender_range = 200;
941 }
942 }
943 max_mididev = 0;
944 seq_mode = SEQ_2;
945}
946
947int sequencer_open(int dev, struct file *file)
948{
949 int retval, mode, i;
950 int level, tmp;
951
952 if (!sequencer_ok)
953 sequencer_init();
954
955 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
956
957 dev = dev >> 4;
958 mode = translate_mode(file);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!sequencer_ok)
961 {
962/* printk("Sound card: sequencer not initialized\n");*/
963 return -ENXIO;
964 }
965 if (dev) /* Patch manager device (obsolete) */
966 return -ENXIO;
967
968 if(synth_devs[dev] == NULL)
969 request_module("synth0");
970
971 if (mode == OPEN_READ)
972 {
973 if (!num_midis)
974 {
975 /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
976 sequencer_busy = 0;
977 return -ENXIO;
978 }
979 }
980 if (sequencer_busy)
981 {
982 return -EBUSY;
983 }
984 sequencer_busy = 1;
985 obsolete_api_used = 0;
986
987 max_mididev = num_midis;
988 max_synthdev = num_synths;
989 pre_event_timeout = MAX_SCHEDULE_TIMEOUT;
990 seq_mode = SEQ_1;
991
992 if (pending_timer != -1)
993 {
994 tmr_no = pending_timer;
995 pending_timer = -1;
996 }
997 if (tmr_no == -1) /* Not selected yet */
998 {
999 int i, best;
1000
1001 best = -1;
1002 for (i = 0; i < num_sound_timers; i++)
1003 if (sound_timer_devs[i] && sound_timer_devs[i]->priority > best)
1004 {
1005 tmr_no = i;
1006 best = sound_timer_devs[i]->priority;
1007 }
1008 if (tmr_no == -1) /* Should not be */
1009 tmr_no = 0;
1010 }
1011 tmr = sound_timer_devs[tmr_no];
1012
1013 if (level == 2)
1014 {
1015 if (tmr == NULL)
1016 {
1017 /*printk("sequencer: No timer for level 2\n");*/
1018 sequencer_busy = 0;
1019 return -ENXIO;
1020 }
1021 setup_mode2();
1022 }
1023 if (!max_synthdev && !max_mididev)
1024 {
1025 sequencer_busy=0;
1026 return -ENXIO;
1027 }
1028
1029 synth_open_mask = 0;
1030
1031 for (i = 0; i < max_mididev; i++)
1032 {
1033 midi_opened[i] = 0;
1034 midi_written[i] = 0;
1035 }
1036
1037 for (i = 0; i < max_synthdev; i++)
1038 {
1039 if (synth_devs[i]==NULL)
1040 continue;
1041
1042 if (!try_module_get(synth_devs[i]->owner))
1043 continue;
1044
1045 if ((tmp = synth_devs[i]->open(i, mode)) < 0)
1046 {
1047 printk(KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1048 if (synth_devs[i]->midi_dev)
1049 printk(KERN_WARNING "(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1050 }
1051 else
1052 {
1053 synth_open_mask |= (1 << i);
1054 if (synth_devs[i]->midi_dev)
1055 midi_opened[synth_devs[i]->midi_dev] = 1;
1056 }
1057 }
1058
1059 seq_time = jiffies;
1060
1061 prev_input_time = 0;
1062 prev_event_time = 0;
1063
1064 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1065 {
1066 /*
1067 * Initialize midi input devices
1068 */
1069
1070 for (i = 0; i < max_mididev; i++)
1071 if (!midi_opened[i] && midi_devs[i])
1072 {
1073 if (!try_module_get(midi_devs[i]->owner))
1074 continue;
1075
1076 if ((retval = midi_devs[i]->open(i, mode,
1077 sequencer_midi_input, sequencer_midi_output)) >= 0)
1078 {
1079 midi_opened[i] = 1;
1080 }
1081 }
1082 }
1083
1084 if (seq_mode == SEQ_2) {
1085 if (try_module_get(tmr->owner))
1086 tmr->open(tmr_no, seq_mode);
1087 }
1088
1089 init_waitqueue_head(&seq_sleeper);
1090 init_waitqueue_head(&midi_sleeper);
1091 output_threshold = SEQ_MAX_QUEUE / 2;
1092
1093 return 0;
1094}
1095
1096static void seq_drain_midi_queues(void)
1097{
1098 int i, n;
1099
1100 /*
1101 * Give the Midi drivers time to drain their output queues
1102 */
1103
1104 n = 1;
1105
1106 while (!signal_pending(current) && n)
1107 {
1108 n = 0;
1109
1110 for (i = 0; i < max_mididev; i++)
1111 if (midi_opened[i] && midi_written[i])
1112 if (midi_devs[i]->buffer_status != NULL)
1113 if (midi_devs[i]->buffer_status(i))
1114 n++;
1115
1116 /*
1117 * Let's have a delay
1118 */
1119
1120 if (n)
Arnd Bergmanncdef2e52014-01-02 13:07:51 +01001121 oss_broken_sleep_on(&seq_sleeper, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123}
1124
1125void sequencer_release(int dev, struct file *file)
1126{
1127 int i;
1128 int mode = translate_mode(file);
1129
1130 dev = dev >> 4;
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /*
1133 * Wait until the queue is empty (if we don't have nonblock)
1134 */
1135
1136 if (mode != OPEN_READ && !(file->f_flags & O_NONBLOCK))
1137 {
1138 while (!signal_pending(current) && qlen > 0)
1139 {
1140 seq_sync();
Arnd Bergmanncdef2e52014-01-02 13:07:51 +01001141 oss_broken_sleep_on(&seq_sleeper, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /* Extra delay */
1143 }
1144 }
1145
1146 if (mode != OPEN_READ)
1147 seq_drain_midi_queues(); /*
1148 * Ensure the output queues are empty
1149 */
1150 seq_reset();
1151 if (mode != OPEN_READ)
1152 seq_drain_midi_queues(); /*
1153 * Flush the all notes off messages
1154 */
1155
1156 for (i = 0; i < max_synthdev; i++)
1157 {
1158 if (synth_open_mask & (1 << i)) /*
1159 * Actually opened
1160 */
1161 if (synth_devs[i])
1162 {
1163 synth_devs[i]->close(i);
1164
1165 module_put(synth_devs[i]->owner);
1166
1167 if (synth_devs[i]->midi_dev)
1168 midi_opened[synth_devs[i]->midi_dev] = 0;
1169 }
1170 }
1171
1172 for (i = 0; i < max_mididev; i++)
1173 {
1174 if (midi_opened[i]) {
1175 midi_devs[i]->close(i);
1176 module_put(midi_devs[i]->owner);
1177 }
1178 }
1179
1180 if (seq_mode == SEQ_2) {
1181 tmr->close(tmr_no);
1182 module_put(tmr->owner);
1183 }
1184
1185 if (obsolete_api_used)
1186 printk(KERN_WARNING "/dev/music: Obsolete (4 byte) API was used by %s\n", current->comm);
1187 sequencer_busy = 0;
1188}
1189
1190static int seq_sync(void)
1191{
1192 if (qlen && !seq_playing && !signal_pending(current))
1193 seq_startplay();
1194
1195 if (qlen > 0)
Arnd Bergmanncdef2e52014-01-02 13:07:51 +01001196 oss_broken_sleep_on(&seq_sleeper, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return qlen;
1198}
1199
1200static void midi_outc(int dev, unsigned char data)
1201{
1202 /*
1203 * NOTE! Calls sleep(). Don't call this from interrupt.
1204 */
1205
1206 int n;
1207 unsigned long flags;
1208
1209 /*
1210 * This routine sends one byte to the Midi channel.
1211 * If the output FIFO is full, it waits until there
1212 * is space in the queue
1213 */
1214
1215 n = 3 * HZ; /* Timeout */
1216
1217 spin_lock_irqsave(&lock,flags);
1218 while (n && !midi_devs[dev]->outputc(dev, data)) {
Arnd Bergmanncdef2e52014-01-02 13:07:51 +01001219 oss_broken_sleep_on(&seq_sleeper, HZ/25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 n--;
1221 }
1222 spin_unlock_irqrestore(&lock,flags);
1223}
1224
1225static void seq_reset(void)
1226{
1227 /*
1228 * NOTE! Calls sleep(). Don't call this from interrupt.
1229 */
1230
1231 int i;
1232 int chn;
1233 unsigned long flags;
1234
1235 sound_stop_timer();
1236
1237 seq_time = jiffies;
1238 prev_input_time = 0;
1239 prev_event_time = 0;
1240
1241 qlen = qhead = qtail = 0;
1242 iqlen = iqhead = iqtail = 0;
1243
1244 for (i = 0; i < max_synthdev; i++)
1245 if (synth_open_mask & (1 << i))
1246 if (synth_devs[i])
1247 synth_devs[i]->reset(i);
1248
1249 if (seq_mode == SEQ_2)
1250 {
1251 for (chn = 0; chn < 16; chn++)
1252 for (i = 0; i < max_synthdev; i++)
1253 if (synth_open_mask & (1 << i))
1254 if (synth_devs[i])
1255 {
1256 synth_devs[i]->controller(i, chn, 123, 0); /* All notes off */
1257 synth_devs[i]->controller(i, chn, 121, 0); /* Reset all ctl */
1258 synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
1259 }
1260 }
1261 else /* seq_mode == SEQ_1 */
1262 {
1263 for (i = 0; i < max_mididev; i++)
1264 if (midi_written[i]) /*
1265 * Midi used. Some notes may still be playing
1266 */
1267 {
1268 /*
1269 * Sending just a ACTIVE SENSING message should be enough to stop all
1270 * playing notes. Since there are devices not recognizing the
1271 * active sensing, we have to send some all notes off messages also.
1272 */
1273 midi_outc(i, 0xfe);
1274
1275 for (chn = 0; chn < 16; chn++)
1276 {
1277 midi_outc(i, (unsigned char) (0xb0 + (chn & 0x0f))); /* control change */
1278 midi_outc(i, 0x7b); /* All notes off */
1279 midi_outc(i, 0); /* Dummy parameter */
1280 }
1281
1282 midi_devs[i]->close(i);
1283
1284 midi_written[i] = 0;
1285 midi_opened[i] = 0;
1286 }
1287 }
1288
1289 seq_playing = 0;
1290
1291 spin_lock_irqsave(&lock,flags);
1292
1293 if (waitqueue_active(&seq_sleeper)) {
1294 /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
1295 wake_up(&seq_sleeper);
1296 }
1297 spin_unlock_irqrestore(&lock,flags);
1298}
1299
1300static void seq_panic(void)
1301{
1302 /*
1303 * This routine is called by the application in case the user
1304 * wants to reset the system to the default state.
1305 */
1306
1307 seq_reset();
1308
1309 /*
1310 * Since some of the devices don't recognize the active sensing and
1311 * all notes off messages, we have to shut all notes manually.
1312 *
1313 * TO BE IMPLEMENTED LATER
1314 */
1315
1316 /*
1317 * Also return the controllers to their default states
1318 */
1319}
1320
1321int sequencer_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
1322{
1323 int midi_dev, orig_dev, val, err;
1324 int mode = translate_mode(file);
1325 struct synth_info inf;
1326 struct seq_event_rec event_rec;
1327 unsigned long flags;
1328 int __user *p = arg;
1329
1330 orig_dev = dev = dev >> 4;
1331
1332 switch (cmd)
1333 {
1334 case SNDCTL_TMR_TIMEBASE:
1335 case SNDCTL_TMR_TEMPO:
1336 case SNDCTL_TMR_START:
1337 case SNDCTL_TMR_STOP:
1338 case SNDCTL_TMR_CONTINUE:
1339 case SNDCTL_TMR_METRONOME:
1340 case SNDCTL_TMR_SOURCE:
1341 if (seq_mode != SEQ_2)
1342 return -EINVAL;
1343 return tmr->ioctl(tmr_no, cmd, arg);
1344
1345 case SNDCTL_TMR_SELECT:
1346 if (seq_mode != SEQ_2)
1347 return -EINVAL;
1348 if (get_user(pending_timer, p))
1349 return -EFAULT;
1350 if (pending_timer < 0 || pending_timer >= num_sound_timers || sound_timer_devs[pending_timer] == NULL)
1351 {
1352 pending_timer = -1;
1353 return -EINVAL;
1354 }
1355 val = pending_timer;
1356 break;
1357
1358 case SNDCTL_SEQ_PANIC:
1359 seq_panic();
1360 return -EINVAL;
1361
1362 case SNDCTL_SEQ_SYNC:
1363 if (mode == OPEN_READ)
1364 return 0;
1365 while (qlen > 0 && !signal_pending(current))
1366 seq_sync();
1367 return qlen ? -EINTR : 0;
1368
1369 case SNDCTL_SEQ_RESET:
1370 seq_reset();
1371 return 0;
1372
1373 case SNDCTL_SEQ_TESTMIDI:
1374 if (__get_user(midi_dev, p))
1375 return -EFAULT;
1376 if (midi_dev < 0 || midi_dev >= max_mididev || !midi_devs[midi_dev])
1377 return -ENXIO;
1378
1379 if (!midi_opened[midi_dev] &&
1380 (err = midi_devs[midi_dev]->open(midi_dev, mode, sequencer_midi_input,
1381 sequencer_midi_output)) < 0)
1382 return err;
1383 midi_opened[midi_dev] = 1;
1384 return 0;
1385
1386 case SNDCTL_SEQ_GETINCOUNT:
1387 if (mode == OPEN_WRITE)
1388 return 0;
1389 val = iqlen;
1390 break;
1391
1392 case SNDCTL_SEQ_GETOUTCOUNT:
1393 if (mode == OPEN_READ)
1394 return 0;
1395 val = SEQ_MAX_QUEUE - qlen;
1396 break;
1397
1398 case SNDCTL_SEQ_GETTIME:
1399 if (seq_mode == SEQ_2)
1400 return tmr->ioctl(tmr_no, cmd, arg);
1401 val = jiffies - seq_time;
1402 break;
1403
1404 case SNDCTL_SEQ_CTRLRATE:
1405 /*
1406 * If *arg == 0, just return the current rate
1407 */
1408 if (seq_mode == SEQ_2)
1409 return tmr->ioctl(tmr_no, cmd, arg);
1410
1411 if (get_user(val, p))
1412 return -EFAULT;
1413 if (val != 0)
1414 return -EINVAL;
1415 val = HZ;
1416 break;
1417
1418 case SNDCTL_SEQ_RESETSAMPLES:
1419 case SNDCTL_SYNTH_REMOVESAMPLE:
1420 case SNDCTL_SYNTH_CONTROL:
1421 if (get_user(dev, p))
1422 return -EFAULT;
1423 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1424 return -ENXIO;
1425 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1426 return -EBUSY;
1427 return synth_devs[dev]->ioctl(dev, cmd, arg);
1428
1429 case SNDCTL_SEQ_NRSYNTHS:
1430 val = max_synthdev;
1431 break;
1432
1433 case SNDCTL_SEQ_NRMIDIS:
1434 val = max_mididev;
1435 break;
1436
1437 case SNDCTL_SYNTH_MEMAVL:
1438 if (get_user(dev, p))
1439 return -EFAULT;
1440 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1441 return -ENXIO;
1442 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1443 return -EBUSY;
1444 val = synth_devs[dev]->ioctl(dev, cmd, arg);
1445 break;
1446
1447 case SNDCTL_FM_4OP_ENABLE:
1448 if (get_user(dev, p))
1449 return -EFAULT;
1450 if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
1451 return -ENXIO;
1452 if (!(synth_open_mask & (1 << dev)))
1453 return -ENXIO;
1454 synth_devs[dev]->ioctl(dev, cmd, arg);
1455 return 0;
1456
1457 case SNDCTL_SYNTH_INFO:
1458 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1459 return -EFAULT;
1460 if (dev < 0 || dev >= max_synthdev)
1461 return -ENXIO;
1462 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1463 return -EBUSY;
1464 return synth_devs[dev]->ioctl(dev, cmd, arg);
1465
1466 /* Like SYNTH_INFO but returns ID in the name field */
1467 case SNDCTL_SYNTH_ID:
1468 if (get_user(dev, &((struct synth_info __user *)arg)->device))
1469 return -EFAULT;
1470 if (dev < 0 || dev >= max_synthdev)
1471 return -ENXIO;
1472 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1473 return -EBUSY;
1474 memcpy(&inf, synth_devs[dev]->info, sizeof(inf));
1475 strlcpy(inf.name, synth_devs[dev]->id, sizeof(inf.name));
1476 inf.device = dev;
1477 return copy_to_user(arg, &inf, sizeof(inf))?-EFAULT:0;
1478
1479 case SNDCTL_SEQ_OUTOFBAND:
1480 if (copy_from_user(&event_rec, arg, sizeof(event_rec)))
1481 return -EFAULT;
1482 spin_lock_irqsave(&lock,flags);
1483 play_event(event_rec.arr);
1484 spin_unlock_irqrestore(&lock,flags);
1485 return 0;
1486
1487 case SNDCTL_MIDI_INFO:
1488 if (get_user(dev, &((struct midi_info __user *)arg)->device))
1489 return -EFAULT;
1490 if (dev < 0 || dev >= max_mididev || !midi_devs[dev])
1491 return -ENXIO;
1492 midi_devs[dev]->info.device = dev;
1493 return copy_to_user(arg, &midi_devs[dev]->info, sizeof(struct midi_info))?-EFAULT:0;
1494
1495 case SNDCTL_SEQ_THRESHOLD:
1496 if (get_user(val, p))
1497 return -EFAULT;
1498 if (val < 1)
1499 val = 1;
1500 if (val >= SEQ_MAX_QUEUE)
1501 val = SEQ_MAX_QUEUE - 1;
1502 output_threshold = val;
1503 return 0;
1504
1505 case SNDCTL_MIDI_PRETIME:
1506 if (get_user(val, p))
1507 return -EFAULT;
1508 if (val < 0)
1509 val = 0;
1510 val = (HZ * val) / 10;
1511 pre_event_timeout = val;
1512 break;
1513
1514 default:
1515 if (mode == OPEN_READ)
1516 return -EIO;
1517 if (!synth_devs[0])
1518 return -ENXIO;
1519 if (!(synth_open_mask & (1 << 0)))
1520 return -ENXIO;
1521 if (!synth_devs[0]->ioctl)
1522 return -EINVAL;
1523 return synth_devs[0]->ioctl(0, cmd, arg);
1524 }
1525 return put_user(val, p);
1526}
1527
1528/* No kernel lock - we're using the global irq lock here */
1529unsigned int sequencer_poll(int dev, struct file *file, poll_table * wait)
1530{
1531 unsigned long flags;
1532 unsigned int mask = 0;
1533
1534 dev = dev >> 4;
1535
1536 spin_lock_irqsave(&lock,flags);
1537 /* input */
1538 poll_wait(file, &midi_sleeper, wait);
1539 if (iqlen)
1540 mask |= POLLIN | POLLRDNORM;
1541
1542 /* output */
1543 poll_wait(file, &seq_sleeper, wait);
1544 if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
1545 mask |= POLLOUT | POLLWRNORM;
1546 spin_unlock_irqrestore(&lock,flags);
1547 return mask;
1548}
1549
1550
1551void sequencer_timer(unsigned long dummy)
1552{
1553 seq_startplay();
1554}
Adrian Bunkece7f772006-10-04 02:17:31 -07001555EXPORT_SYMBOL(sequencer_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557int note_to_freq(int note_num)
1558{
1559
1560 /*
1561 * This routine converts a midi note to a frequency (multiplied by 1000)
1562 */
1563
1564 int note, octave, note_freq;
1565 static int notes[] =
1566 {
1567 261632, 277189, 293671, 311132, 329632, 349232,
1568 369998, 391998, 415306, 440000, 466162, 493880
1569 };
1570
1571#define BASE_OCTAVE 5
1572
1573 octave = note_num / 12;
1574 note = note_num % 12;
1575
1576 note_freq = notes[note];
1577
1578 if (octave < BASE_OCTAVE)
1579 note_freq >>= (BASE_OCTAVE - octave);
1580 else if (octave > BASE_OCTAVE)
1581 note_freq <<= (octave - BASE_OCTAVE);
1582
1583 /*
1584 * note_freq >>= 1;
1585 */
1586
1587 return note_freq;
1588}
Adrian Bunkece7f772006-10-04 02:17:31 -07001589EXPORT_SYMBOL(note_to_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591unsigned long compute_finetune(unsigned long base_freq, int bend, int range,
1592 int vibrato_cents)
1593{
1594 unsigned long amount;
1595 int negative, semitones, cents, multiplier = 1;
1596
1597 if (!bend)
1598 return base_freq;
1599 if (!range)
1600 return base_freq;
1601
1602 if (!base_freq)
1603 return base_freq;
1604
1605 if (range >= 8192)
1606 range = 8192;
1607
1608 bend = bend * range / 8192; /* Convert to cents */
1609 bend += vibrato_cents;
1610
1611 if (!bend)
1612 return base_freq;
1613
1614 negative = bend < 0 ? 1 : 0;
1615
1616 if (bend < 0)
1617 bend *= -1;
1618 if (bend > range)
1619 bend = range;
1620
1621 /*
1622 if (bend > 2399)
1623 bend = 2399;
1624 */
1625 while (bend > 2399)
1626 {
1627 multiplier *= 4;
1628 bend -= 2400;
1629 }
1630
1631 semitones = bend / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 cents = bend % 100;
1633
1634 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
1635
1636 if (negative)
1637 return (base_freq * 10000) / amount; /* Bend down */
1638 else
1639 return (base_freq * amount) / 10000; /* Bend up */
1640}
Adrian Bunkece7f772006-10-04 02:17:31 -07001641EXPORT_SYMBOL(compute_finetune);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643void sequencer_init(void)
1644{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (sequencer_ok)
1646 return;
Jesper Juhlea7dd252010-11-09 00:11:03 +01001647 queue = vmalloc(SEQ_MAX_QUEUE * EV_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (queue == NULL)
1649 {
1650 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n");
1651 return;
1652 }
Jesper Juhlea7dd252010-11-09 00:11:03 +01001653 iqueue = vmalloc(SEQ_MAX_QUEUE * IEV_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (iqueue == NULL)
1655 {
1656 printk(KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n");
1657 vfree(queue);
1658 return;
1659 }
1660 sequencer_ok = 1;
1661}
Adrian Bunkece7f772006-10-04 02:17:31 -07001662EXPORT_SYMBOL(sequencer_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664void sequencer_unload(void)
1665{
Jesper Juhl5a83fdd2006-03-28 01:56:50 -08001666 vfree(queue);
1667 vfree(iqueue);
1668 queue = iqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}