~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/oss/sequencer.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.10 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.83 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.153 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.200 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.200 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.76 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp