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