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