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