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