diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /sound/oss/sequencer.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'sound/oss/sequencer.c')
-rw-r--r-- | sound/oss/sequencer.c | 1684 |
1 files changed, 1684 insertions, 0 deletions
diff --git a/sound/oss/sequencer.c b/sound/oss/sequencer.c new file mode 100644 index 000000000000..698614226c9a --- /dev/null +++ b/sound/oss/sequencer.c | |||
@@ -0,0 +1,1684 @@ | |||
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 | #include <linux/spinlock.h> | ||
19 | #define SEQUENCER_C | ||
20 | #include "sound_config.h" | ||
21 | |||
22 | #include "midi_ctrl.h" | ||
23 | |||
24 | static int sequencer_ok; | ||
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; | ||
31 | static DEFINE_SPINLOCK(lock); | ||
32 | |||
33 | /* | ||
34 | * Local counts for number of synth and MIDI devices. These are initialized | ||
35 | * by the sequencer_open. | ||
36 | */ | ||
37 | static int max_mididev; | ||
38 | static int max_synthdev; | ||
39 | |||
40 | /* | ||
41 | * The seq_mode gives the operating mode of the sequencer: | ||
42 | * 1 = level1 (the default) | ||
43 | * 2 = level2 (extended capabilities) | ||
44 | */ | ||
45 | |||
46 | #define SEQ_1 1 | ||
47 | #define SEQ_2 2 | ||
48 | static int seq_mode = SEQ_1; | ||
49 | |||
50 | static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper); | ||
51 | static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper); | ||
52 | |||
53 | static int midi_opened[MAX_MIDI_DEV]; | ||
54 | |||
55 | static int midi_written[MAX_MIDI_DEV]; | ||
56 | |||
57 | static unsigned long prev_input_time; | ||
58 | static int prev_event_time; | ||
59 | |||
60 | #include "tuning.h" | ||
61 | |||
62 | #define EV_SZ 8 | ||
63 | #define IEV_SZ 8 | ||
64 | |||
65 | static unsigned char *queue; | ||
66 | static unsigned char *iqueue; | ||
67 | |||
68 | static volatile int qhead, qtail, qlen; | ||
69 | static volatile int iqhead, iqtail, iqlen; | ||
70 | static volatile int seq_playing; | ||
71 | static volatile int sequencer_busy; | ||
72 | static int output_threshold; | ||
73 | static long pre_event_timeout; | ||
74 | static unsigned synth_open_mask; | ||
75 | |||
76 | static int seq_queue(unsigned char *note, char nonblock); | ||
77 | static void seq_startplay(void); | ||
78 | static int seq_sync(void); | ||
79 | static void seq_reset(void); | ||
80 | |||
81 | #if MAX_SYNTH_DEV > 15 | ||
82 | #error Too many synthesizer devices enabled. | ||
83 | #endif | ||
84 | |||
85 | int sequencer_read(int dev, struct file *file, char __user *buf, int count) | ||
86 | { | ||
87 | int c = count, p = 0; | ||
88 | int ev_len; | ||
89 | unsigned long flags; | ||
90 | |||
91 | dev = dev >> 4; | ||
92 | |||
93 | ev_len = seq_mode == SEQ_1 ? 4 : 8; | ||
94 | |||
95 | spin_lock_irqsave(&lock,flags); | ||
96 | |||
97 | if (!iqlen) | ||
98 | { | ||
99 | spin_unlock_irqrestore(&lock,flags); | ||
100 | if (file->f_flags & O_NONBLOCK) { | ||
101 | return -EAGAIN; | ||
102 | } | ||
103 | |||
104 | interruptible_sleep_on_timeout(&midi_sleeper, | ||
105 | pre_event_timeout); | ||
106 | spin_lock_irqsave(&lock,flags); | ||
107 | if (!iqlen) | ||
108 | { | ||
109 | spin_unlock_irqrestore(&lock,flags); | ||
110 | return 0; | ||
111 | } | ||
112 | } | ||
113 | while (iqlen && c >= ev_len) | ||
114 | { | ||
115 | char *fixit = (char *) &iqueue[iqhead * IEV_SZ]; | ||
116 | spin_unlock_irqrestore(&lock,flags); | ||
117 | if (copy_to_user(&(buf)[p], fixit, ev_len)) | ||
118 | return count - c; | ||
119 | p += ev_len; | ||
120 | c -= ev_len; | ||
121 | |||
122 | spin_lock_irqsave(&lock,flags); | ||
123 | iqhead = (iqhead + 1) % SEQ_MAX_QUEUE; | ||
124 | iqlen--; | ||
125 | } | ||
126 | spin_unlock_irqrestore(&lock,flags); | ||
127 | return count - c; | ||
128 | } | ||
129 | |||
130 | static void sequencer_midi_output(int dev) | ||
131 | { | ||
132 | /* | ||
133 | * Currently NOP | ||
134 | */ | ||
135 | } | ||
136 | |||
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 | spin_lock_irqsave(&lock,flags); | ||
154 | memcpy(&iqueue[iqtail * IEV_SZ], event_rec, len); | ||
155 | iqlen++; | ||
156 | iqtail = (iqtail + 1) % SEQ_MAX_QUEUE; | ||
157 | wake_up(&midi_sleeper); | ||
158 | spin_unlock_irqrestore(&lock,flags); | ||
159 | } | ||
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 | |||
210 | int sequencer_write(int dev, struct file *file, const char __user *buf, int count) | ||
211 | { | ||
212 | unsigned char event_rec[EV_SZ], ev_code; | ||
213 | int p = 0, c, ev_size; | ||
214 | int err; | ||
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 + 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 | 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 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 | if (seq_mode == SEQ_2) | ||
681 | seq_copy_to_input(event_rec, 8); | ||
682 | else | ||
683 | { | ||
684 | parm = (parm << 8 | SEQ_ECHO); | ||
685 | seq_copy_to_input((unsigned char *) &parm, 4); | ||
686 | } | ||
687 | break; | ||
688 | |||
689 | default:; | ||
690 | } | ||
691 | |||
692 | return TIMER_NOT_ARMED; | ||
693 | } | ||
694 | |||
695 | static void seq_local_event(unsigned char *event_rec) | ||
696 | { | ||
697 | unsigned char cmd = event_rec[1]; | ||
698 | unsigned int parm = *((unsigned int *) &event_rec[4]); | ||
699 | |||
700 | switch (cmd) | ||
701 | { | ||
702 | case LOCL_STARTAUDIO: | ||
703 | DMAbuf_start_devices(parm); | ||
704 | break; | ||
705 | |||
706 | default:; | ||
707 | } | ||
708 | } | ||
709 | |||
710 | static void seq_sysex_message(unsigned char *event_rec) | ||
711 | { | ||
712 | int dev = event_rec[1]; | ||
713 | int i, l = 0; | ||
714 | unsigned char *buf = &event_rec[2]; | ||
715 | |||
716 | if ((int) dev > max_synthdev) | ||
717 | return; | ||
718 | if (!(synth_open_mask & (1 << dev))) | ||
719 | return; | ||
720 | if (!synth_devs[dev]) | ||
721 | return; | ||
722 | |||
723 | l = 0; | ||
724 | for (i = 0; i < 6 && buf[i] != 0xff; i++) | ||
725 | l = i + 1; | ||
726 | |||
727 | if (!synth_devs[dev]->send_sysex) | ||
728 | return; | ||
729 | if (l > 0) | ||
730 | synth_devs[dev]->send_sysex(dev, buf, l); | ||
731 | } | ||
732 | |||
733 | static int play_event(unsigned char *q) | ||
734 | { | ||
735 | /* | ||
736 | * NOTE! This routine returns | ||
737 | * 0 = normal event played. | ||
738 | * 1 = Timer armed. Suspend playback until timer callback. | ||
739 | * 2 = MIDI output buffer full. Restore queue and suspend until timer | ||
740 | */ | ||
741 | unsigned int *delay; | ||
742 | |||
743 | switch (q[0]) | ||
744 | { | ||
745 | case SEQ_NOTEOFF: | ||
746 | if (synth_open_mask & (1 << 0)) | ||
747 | if (synth_devs[0]) | ||
748 | synth_devs[0]->kill_note(0, q[1], 255, q[3]); | ||
749 | break; | ||
750 | |||
751 | case SEQ_NOTEON: | ||
752 | if (q[4] < 128 || q[4] == 255) | ||
753 | if (synth_open_mask & (1 << 0)) | ||
754 | if (synth_devs[0]) | ||
755 | synth_devs[0]->start_note(0, q[1], q[2], q[3]); | ||
756 | break; | ||
757 | |||
758 | case SEQ_WAIT: | ||
759 | delay = (unsigned int *) q; /* | ||
760 | * Bytes 1 to 3 are containing the * | ||
761 | * delay in 'ticks' | ||
762 | */ | ||
763 | *delay = (*delay >> 8) & 0xffffff; | ||
764 | |||
765 | if (*delay > 0) | ||
766 | { | ||
767 | long time; | ||
768 | |||
769 | seq_playing = 1; | ||
770 | time = *delay; | ||
771 | prev_event_time = time; | ||
772 | |||
773 | request_sound_timer(time); | ||
774 | |||
775 | if ((SEQ_MAX_QUEUE - qlen) >= output_threshold) | ||
776 | wake_up(&seq_sleeper); | ||
777 | /* | ||
778 | * The timer is now active and will reinvoke this function | ||
779 | * after the timer expires. Return to the caller now. | ||
780 | */ | ||
781 | return 1; | ||
782 | } | ||
783 | break; | ||
784 | |||
785 | case SEQ_PGMCHANGE: | ||
786 | if (synth_open_mask & (1 << 0)) | ||
787 | if (synth_devs[0]) | ||
788 | synth_devs[0]->set_instr(0, q[1], q[2]); | ||
789 | break; | ||
790 | |||
791 | case SEQ_SYNCTIMER: /* | ||
792 | * Reset timer | ||
793 | */ | ||
794 | seq_time = jiffies; | ||
795 | prev_input_time = 0; | ||
796 | prev_event_time = 0; | ||
797 | break; | ||
798 | |||
799 | case SEQ_MIDIPUTC: /* | ||
800 | * Put a midi character | ||
801 | */ | ||
802 | if (midi_opened[q[2]]) | ||
803 | { | ||
804 | int dev; | ||
805 | |||
806 | dev = q[2]; | ||
807 | |||
808 | if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL) | ||
809 | break; | ||
810 | |||
811 | if (!midi_devs[dev]->outputc(dev, q[1])) | ||
812 | { | ||
813 | /* | ||
814 | * Output FIFO is full. Wait one timer cycle and try again. | ||
815 | */ | ||
816 | |||
817 | seq_playing = 1; | ||
818 | request_sound_timer(-1); | ||
819 | return 2; | ||
820 | } | ||
821 | else | ||
822 | midi_written[dev] = 1; | ||
823 | } | ||
824 | break; | ||
825 | |||
826 | case SEQ_ECHO: | ||
827 | seq_copy_to_input(q, 4); /* | ||
828 | * Echo back to the process | ||
829 | */ | ||
830 | break; | ||
831 | |||
832 | case SEQ_PRIVATE: | ||
833 | if ((int) q[1] < max_synthdev) | ||
834 | synth_devs[q[1]]->hw_control(q[1], q); | ||
835 | break; | ||
836 | |||
837 | case SEQ_EXTENDED: | ||
838 | extended_event(q); | ||
839 | break; | ||
840 | |||
841 | case EV_CHN_VOICE: | ||
842 | seq_chn_voice_event(q); | ||
843 | break; | ||
844 | |||
845 | case EV_CHN_COMMON: | ||
846 | seq_chn_common_event(q); | ||
847 | break; | ||
848 | |||
849 | case EV_TIMING: | ||
850 | if (seq_timing_event(q) == TIMER_ARMED) | ||
851 | { | ||
852 | return 1; | ||
853 | } | ||
854 | break; | ||
855 | |||
856 | case EV_SEQ_LOCAL: | ||
857 | seq_local_event(q); | ||
858 | break; | ||
859 | |||
860 | case EV_SYSEX: | ||
861 | seq_sysex_message(q); | ||
862 | break; | ||
863 | |||
864 | default:; | ||
865 | } | ||
866 | return 0; | ||
867 | } | ||
868 | |||
869 | /* called also as timer in irq context */ | ||
870 | static void seq_startplay(void) | ||
871 | { | ||
872 | int this_one, action; | ||
873 | unsigned long flags; | ||
874 | |||
875 | while (qlen > 0) | ||
876 | { | ||
877 | |||
878 | spin_lock_irqsave(&lock,flags); | ||
879 | qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE; | ||
880 | qlen--; | ||
881 | spin_unlock_irqrestore(&lock,flags); | ||
882 | |||
883 | seq_playing = 1; | ||
884 | |||
885 | if ((action = play_event(&queue[this_one * EV_SZ]))) | ||
886 | { /* Suspend playback. Next timer routine invokes this routine again */ | ||
887 | if (action == 2) | ||
888 | { | ||
889 | qlen++; | ||
890 | qhead = this_one; | ||
891 | } | ||
892 | return; | ||
893 | } | ||
894 | } | ||
895 | |||
896 | seq_playing = 0; | ||
897 | |||
898 | if ((SEQ_MAX_QUEUE - qlen) >= output_threshold) | ||
899 | wake_up(&seq_sleeper); | ||
900 | } | ||
901 | |||
902 | static void reset_controllers(int dev, unsigned char *controller, int update_dev) | ||
903 | { | ||
904 | int i; | ||
905 | for (i = 0; i < 128; i++) | ||
906 | controller[i] = ctrl_def_values[i]; | ||
907 | } | ||
908 | |||
909 | static void setup_mode2(void) | ||
910 | { | ||
911 | int dev; | ||
912 | |||
913 | max_synthdev = num_synths; | ||
914 | |||
915 | for (dev = 0; dev < num_midis; dev++) | ||
916 | { | ||
917 | if (midi_devs[dev] && midi_devs[dev]->converter != NULL) | ||
918 | { | ||
919 | synth_devs[max_synthdev++] = midi_devs[dev]->converter; | ||
920 | } | ||
921 | } | ||
922 | |||
923 | for (dev = 0; dev < max_synthdev; dev++) | ||
924 | { | ||
925 | int chn; | ||
926 | |||
927 | synth_devs[dev]->sysex_ptr = 0; | ||
928 | synth_devs[dev]->emulation = 0; | ||
929 | |||
930 | for (chn = 0; chn < 16; chn++) | ||
931 | { | ||
932 | synth_devs[dev]->chn_info[chn].pgm_num = 0; | ||
933 | reset_controllers(dev, | ||
934 | synth_devs[dev]->chn_info[chn].controllers,0); | ||
935 | synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */ | ||
936 | synth_devs[dev]->chn_info[chn].bender_range = 200; | ||
937 | } | ||
938 | } | ||
939 | max_mididev = 0; | ||
940 | seq_mode = SEQ_2; | ||
941 | } | ||
942 | |||
943 | int sequencer_open(int dev, struct file *file) | ||
944 | { | ||
945 | int retval, mode, i; | ||
946 | int level, tmp; | ||
947 | |||
948 | if (!sequencer_ok) | ||
949 | sequencer_init(); | ||
950 | |||
951 | level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1; | ||
952 | |||
953 | dev = dev >> 4; | ||
954 | mode = translate_mode(file); | ||
955 | |||
956 | DEB(printk("sequencer_open(dev=%d)\n", dev)); | ||
957 | |||
958 | if (!sequencer_ok) | ||
959 | { | ||
960 | /* printk("Sound card: sequencer not initialized\n");*/ | ||
961 | return -ENXIO; | ||
962 | } | ||
963 | if (dev) /* Patch manager device (obsolete) */ | ||
964 | return -ENXIO; | ||
965 | |||
966 | if(synth_devs[dev] == NULL) | ||
967 | request_module("synth0"); | ||
968 | |||
969 | if (mode == OPEN_READ) | ||
970 | { | ||
971 | if (!num_midis) | ||
972 | { | ||
973 | /*printk("Sequencer: No MIDI devices. Input not possible\n");*/ | ||
974 | sequencer_busy = 0; | ||
975 | return -ENXIO; | ||
976 | } | ||
977 | } | ||
978 | if (sequencer_busy) | ||
979 | { | ||
980 | return -EBUSY; | ||
981 | } | ||
982 | sequencer_busy = 1; | ||
983 | obsolete_api_used = 0; | ||
984 | |||
985 | max_mididev = num_midis; | ||
986 | max_synthdev = num_synths; | ||
987 | pre_event_timeout = MAX_SCHEDULE_TIMEOUT; | ||
988 | seq_mode = SEQ_1; | ||
989 | |||
990 | if (pending_timer != -1) | ||
991 | { | ||
992 | tmr_no = pending_timer; | ||
993 | pending_timer = -1; | ||
994 | } | ||
995 | if (tmr_no == -1) /* Not selected yet */ | ||
996 | { | ||
997 | int i, best; | ||
998 | |||
999 | best = -1; | ||
1000 | for (i = 0; i < num_sound_timers; i++) | ||
1001 | if (sound_timer_devs[i] && sound_timer_devs[i]->priority > best) | ||
1002 | { | ||
1003 | tmr_no = i; | ||
1004 | best = sound_timer_devs[i]->priority; | ||
1005 | } | ||
1006 | if (tmr_no == -1) /* Should not be */ | ||
1007 | tmr_no = 0; | ||
1008 | } | ||
1009 | tmr = sound_timer_devs[tmr_no]; | ||
1010 | |||
1011 | if (level == 2) | ||
1012 | { | ||
1013 | if (tmr == NULL) | ||
1014 | { | ||
1015 | /*printk("sequencer: No timer for level 2\n");*/ | ||
1016 | sequencer_busy = 0; | ||
1017 | return -ENXIO; | ||
1018 | } | ||
1019 | setup_mode2(); | ||
1020 | } | ||
1021 | if (!max_synthdev && !max_mididev) | ||
1022 | { | ||
1023 | sequencer_busy=0; | ||
1024 | return -ENXIO; | ||
1025 | } | ||
1026 | |||
1027 | synth_open_mask = 0; | ||
1028 | |||
1029 | for (i = 0; i < max_mididev; i++) | ||
1030 | { | ||
1031 | midi_opened[i] = 0; | ||
1032 | midi_written[i] = 0; | ||
1033 | } | ||
1034 | |||
1035 | for (i = 0; i < max_synthdev; i++) | ||
1036 | { | ||
1037 | if (synth_devs[i]==NULL) | ||
1038 | continue; | ||
1039 | |||
1040 | if (!try_module_get(synth_devs[i]->owner)) | ||
1041 | continue; | ||
1042 | |||
1043 | if ((tmp = synth_devs[i]->open(i, mode)) < 0) | ||
1044 | { | ||
1045 | printk(KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp); | ||
1046 | if (synth_devs[i]->midi_dev) | ||
1047 | printk(KERN_WARNING "(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev); | ||
1048 | } | ||
1049 | else | ||
1050 | { | ||
1051 | synth_open_mask |= (1 << i); | ||
1052 | if (synth_devs[i]->midi_dev) | ||
1053 | midi_opened[synth_devs[i]->midi_dev] = 1; | ||
1054 | } | ||
1055 | } | ||
1056 | |||
1057 | seq_time = jiffies; | ||
1058 | |||
1059 | prev_input_time = 0; | ||
1060 | prev_event_time = 0; | ||
1061 | |||
1062 | if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE)) | ||
1063 | { | ||
1064 | /* | ||
1065 | * Initialize midi input devices | ||
1066 | */ | ||
1067 | |||
1068 | for (i = 0; i < max_mididev; i++) | ||
1069 | if (!midi_opened[i] && midi_devs[i]) | ||
1070 | { | ||
1071 | if (!try_module_get(midi_devs[i]->owner)) | ||
1072 | continue; | ||
1073 | |||
1074 | if ((retval = midi_devs[i]->open(i, mode, | ||
1075 | sequencer_midi_input, sequencer_midi_output)) >= 0) | ||
1076 | { | ||
1077 | midi_opened[i] = 1; | ||
1078 | } | ||
1079 | } | ||
1080 | } | ||
1081 | |||
1082 | if (seq_mode == SEQ_2) { | ||
1083 | if (try_module_get(tmr->owner)) | ||
1084 | tmr->open(tmr_no, seq_mode); | ||
1085 | } | ||
1086 | |||
1087 | init_waitqueue_head(&seq_sleeper); | ||
1088 | init_waitqueue_head(&midi_sleeper); | ||
1089 | output_threshold = SEQ_MAX_QUEUE / 2; | ||
1090 | |||
1091 | return 0; | ||
1092 | } | ||
1093 | |||
1094 | static void seq_drain_midi_queues(void) | ||
1095 | { | ||
1096 | int i, n; | ||
1097 | |||
1098 | /* | ||
1099 | * Give the Midi drivers time to drain their output queues | ||
1100 | */ | ||
1101 | |||
1102 | n = 1; | ||
1103 | |||
1104 | while (!signal_pending(current) && n) | ||
1105 | { | ||
1106 | n = 0; | ||
1107 | |||
1108 | for (i = 0; i < max_mididev; i++) | ||
1109 | if (midi_opened[i] && midi_written[i]) | ||
1110 | if (midi_devs[i]->buffer_status != NULL) | ||
1111 | if (midi_devs[i]->buffer_status(i)) | ||
1112 | n++; | ||
1113 | |||
1114 | /* | ||
1115 | * Let's have a delay | ||
1116 | */ | ||
1117 | |||
1118 | if (n) | ||
1119 | interruptible_sleep_on_timeout(&seq_sleeper, | ||
1120 | HZ/10); | ||
1121 | } | ||
1122 | } | ||
1123 | |||
1124 | void sequencer_release(int dev, struct file *file) | ||
1125 | { | ||
1126 | int i; | ||
1127 | int mode = translate_mode(file); | ||
1128 | |||
1129 | dev = dev >> 4; | ||
1130 | |||
1131 | DEB(printk("sequencer_release(dev=%d)\n", dev)); | ||
1132 | |||
1133 | /* | ||
1134 | * Wait until the queue is empty (if we don't have nonblock) | ||
1135 | */ | ||
1136 | |||
1137 | if (mode != OPEN_READ && !(file->f_flags & O_NONBLOCK)) | ||
1138 | { | ||
1139 | while (!signal_pending(current) && qlen > 0) | ||
1140 | { | ||
1141 | seq_sync(); | ||
1142 | interruptible_sleep_on_timeout(&seq_sleeper, | ||
1143 | 3*HZ); | ||
1144 | /* Extra delay */ | ||
1145 | } | ||
1146 | } | ||
1147 | |||
1148 | if (mode != OPEN_READ) | ||
1149 | seq_drain_midi_queues(); /* | ||
1150 | * Ensure the output queues are empty | ||
1151 | */ | ||
1152 | seq_reset(); | ||
1153 | if (mode != OPEN_READ) | ||
1154 | seq_drain_midi_queues(); /* | ||
1155 | * Flush the all notes off messages | ||
1156 | */ | ||
1157 | |||
1158 | for (i = 0; i < max_synthdev; i++) | ||
1159 | { | ||
1160 | if (synth_open_mask & (1 << i)) /* | ||
1161 | * Actually opened | ||
1162 | */ | ||
1163 | if (synth_devs[i]) | ||
1164 | { | ||
1165 | synth_devs[i]->close(i); | ||
1166 | |||
1167 | module_put(synth_devs[i]->owner); | ||
1168 | |||
1169 | if (synth_devs[i]->midi_dev) | ||
1170 | midi_opened[synth_devs[i]->midi_dev] = 0; | ||
1171 | } | ||
1172 | } | ||
1173 | |||
1174 | for (i = 0; i < max_mididev; i++) | ||
1175 | { | ||
1176 | if (midi_opened[i]) { | ||
1177 | midi_devs[i]->close(i); | ||
1178 | module_put(midi_devs[i]->owner); | ||
1179 | } | ||
1180 | } | ||
1181 | |||
1182 | if (seq_mode == SEQ_2) { | ||
1183 | tmr->close(tmr_no); | ||
1184 | module_put(tmr->owner); | ||
1185 | } | ||
1186 | |||
1187 | if (obsolete_api_used) | ||
1188 | printk(KERN_WARNING "/dev/music: Obsolete (4 byte) API was used by %s\n", current->comm); | ||
1189 | sequencer_busy = 0; | ||
1190 | } | ||
1191 | |||
1192 | static int seq_sync(void) | ||
1193 | { | ||
1194 | if (qlen && !seq_playing && !signal_pending(current)) | ||
1195 | seq_startplay(); | ||
1196 | |||
1197 | if (qlen > 0) | ||
1198 | interruptible_sleep_on_timeout(&seq_sleeper, HZ); | ||
1199 | return qlen; | ||
1200 | } | ||
1201 | |||
1202 | static void midi_outc(int dev, unsigned char data) | ||
1203 | { | ||
1204 | /* | ||
1205 | * NOTE! Calls sleep(). Don't call this from interrupt. | ||
1206 | */ | ||
1207 | |||
1208 | int n; | ||
1209 | unsigned long flags; | ||
1210 | |||
1211 | /* | ||
1212 | * This routine sends one byte to the Midi channel. | ||
1213 | * If the output FIFO is full, it waits until there | ||
1214 | * is space in the queue | ||
1215 | */ | ||
1216 | |||
1217 | n = 3 * HZ; /* Timeout */ | ||
1218 | |||
1219 | spin_lock_irqsave(&lock,flags); | ||
1220 | while (n && !midi_devs[dev]->outputc(dev, data)) { | ||
1221 | interruptible_sleep_on_timeout(&seq_sleeper, HZ/25); | ||
1222 | n--; | ||
1223 | } | ||
1224 | spin_unlock_irqrestore(&lock,flags); | ||
1225 | } | ||
1226 | |||
1227 | static void seq_reset(void) | ||
1228 | { | ||
1229 | /* | ||
1230 | * NOTE! Calls sleep(). Don't call this from interrupt. | ||
1231 | */ | ||
1232 | |||
1233 | int i; | ||
1234 | int chn; | ||
1235 | unsigned long flags; | ||
1236 | |||
1237 | sound_stop_timer(); | ||
1238 | |||
1239 | seq_time = jiffies; | ||
1240 | prev_input_time = 0; | ||
1241 | prev_event_time = 0; | ||
1242 | |||
1243 | qlen = qhead = qtail = 0; | ||
1244 | iqlen = iqhead = iqtail = 0; | ||
1245 | |||
1246 | for (i = 0; i < max_synthdev; i++) | ||
1247 | if (synth_open_mask & (1 << i)) | ||
1248 | if (synth_devs[i]) | ||
1249 | synth_devs[i]->reset(i); | ||
1250 | |||
1251 | if (seq_mode == SEQ_2) | ||
1252 | { | ||
1253 | for (chn = 0; chn < 16; chn++) | ||
1254 | for (i = 0; i < max_synthdev; i++) | ||
1255 | if (synth_open_mask & (1 << i)) | ||
1256 | if (synth_devs[i]) | ||
1257 | { | ||
1258 | synth_devs[i]->controller(i, chn, 123, 0); /* All notes off */ | ||
1259 | synth_devs[i]->controller(i, chn, 121, 0); /* Reset all ctl */ | ||
1260 | synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */ | ||
1261 | } | ||
1262 | } | ||
1263 | else /* seq_mode == SEQ_1 */ | ||
1264 | { | ||
1265 | for (i = 0; i < max_mididev; i++) | ||
1266 | if (midi_written[i]) /* | ||
1267 | * Midi used. Some notes may still be playing | ||
1268 | */ | ||
1269 | { | ||
1270 | /* | ||
1271 | * Sending just a ACTIVE SENSING message should be enough to stop all | ||
1272 | * playing notes. Since there are devices not recognizing the | ||
1273 | * active sensing, we have to send some all notes off messages also. | ||
1274 | */ | ||
1275 | midi_outc(i, 0xfe); | ||
1276 | |||
1277 | for (chn = 0; chn < 16; chn++) | ||
1278 | { | ||
1279 | midi_outc(i, (unsigned char) (0xb0 + (chn & 0x0f))); /* control change */ | ||
1280 | midi_outc(i, 0x7b); /* All notes off */ | ||
1281 | midi_outc(i, 0); /* Dummy parameter */ | ||
1282 | } | ||
1283 | |||
1284 | midi_devs[i]->close(i); | ||
1285 | |||
1286 | midi_written[i] = 0; | ||
1287 | midi_opened[i] = 0; | ||
1288 | } | ||
1289 | } | ||
1290 | |||
1291 | seq_playing = 0; | ||
1292 | |||
1293 | spin_lock_irqsave(&lock,flags); | ||
1294 | |||
1295 | if (waitqueue_active(&seq_sleeper)) { | ||
1296 | /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */ | ||
1297 | wake_up(&seq_sleeper); | ||
1298 | } | ||
1299 | spin_unlock_irqrestore(&lock,flags); | ||
1300 | } | ||
1301 | |||
1302 | static void seq_panic(void) | ||
1303 | { | ||
1304 | /* | ||
1305 | * This routine is called by the application in case the user | ||
1306 | * wants to reset the system to the default state. | ||
1307 | */ | ||
1308 | |||
1309 | seq_reset(); | ||
1310 | |||
1311 | /* | ||
1312 | * Since some of the devices don't recognize the active sensing and | ||
1313 | * all notes off messages, we have to shut all notes manually. | ||
1314 | * | ||
1315 | * TO BE IMPLEMENTED LATER | ||
1316 | */ | ||
1317 | |||
1318 | /* | ||
1319 | * Also return the controllers to their default states | ||
1320 | */ | ||
1321 | } | ||
1322 | |||
1323 | int sequencer_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg) | ||
1324 | { | ||
1325 | int midi_dev, orig_dev, val, err; | ||
1326 | int mode = translate_mode(file); | ||
1327 | struct synth_info inf; | ||
1328 | struct seq_event_rec event_rec; | ||
1329 | unsigned long flags; | ||
1330 | int __user *p = arg; | ||
1331 | |||
1332 | orig_dev = dev = dev >> 4; | ||
1333 | |||
1334 | switch (cmd) | ||
1335 | { | ||
1336 | case SNDCTL_TMR_TIMEBASE: | ||
1337 | case SNDCTL_TMR_TEMPO: | ||
1338 | case SNDCTL_TMR_START: | ||
1339 | case SNDCTL_TMR_STOP: | ||
1340 | case SNDCTL_TMR_CONTINUE: | ||
1341 | case SNDCTL_TMR_METRONOME: | ||
1342 | case SNDCTL_TMR_SOURCE: | ||
1343 | if (seq_mode != SEQ_2) | ||
1344 | return -EINVAL; | ||
1345 | return tmr->ioctl(tmr_no, cmd, arg); | ||
1346 | |||
1347 | case SNDCTL_TMR_SELECT: | ||
1348 | if (seq_mode != SEQ_2) | ||
1349 | return -EINVAL; | ||
1350 | if (get_user(pending_timer, p)) | ||
1351 | return -EFAULT; | ||
1352 | if (pending_timer < 0 || pending_timer >= num_sound_timers || sound_timer_devs[pending_timer] == NULL) | ||
1353 | { | ||
1354 | pending_timer = -1; | ||
1355 | return -EINVAL; | ||
1356 | } | ||
1357 | val = pending_timer; | ||
1358 | break; | ||
1359 | |||
1360 | case SNDCTL_SEQ_PANIC: | ||
1361 | seq_panic(); | ||
1362 | return -EINVAL; | ||
1363 | |||
1364 | case SNDCTL_SEQ_SYNC: | ||
1365 | if (mode == OPEN_READ) | ||
1366 | return 0; | ||
1367 | while (qlen > 0 && !signal_pending(current)) | ||
1368 | seq_sync(); | ||
1369 | return qlen ? -EINTR : 0; | ||
1370 | |||
1371 | case SNDCTL_SEQ_RESET: | ||
1372 | seq_reset(); | ||
1373 | return 0; | ||
1374 | |||
1375 | case SNDCTL_SEQ_TESTMIDI: | ||
1376 | if (__get_user(midi_dev, p)) | ||
1377 | return -EFAULT; | ||
1378 | if (midi_dev < 0 || midi_dev >= max_mididev || !midi_devs[midi_dev]) | ||
1379 | return -ENXIO; | ||
1380 | |||
1381 | if (!midi_opened[midi_dev] && | ||
1382 | (err = midi_devs[midi_dev]->open(midi_dev, mode, sequencer_midi_input, | ||
1383 | sequencer_midi_output)) < 0) | ||
1384 | return err; | ||
1385 | midi_opened[midi_dev] = 1; | ||
1386 | return 0; | ||
1387 | |||
1388 | case SNDCTL_SEQ_GETINCOUNT: | ||
1389 | if (mode == OPEN_WRITE) | ||
1390 | return 0; | ||
1391 | val = iqlen; | ||
1392 | break; | ||
1393 | |||
1394 | case SNDCTL_SEQ_GETOUTCOUNT: | ||
1395 | if (mode == OPEN_READ) | ||
1396 | return 0; | ||
1397 | val = SEQ_MAX_QUEUE - qlen; | ||
1398 | break; | ||
1399 | |||
1400 | case SNDCTL_SEQ_GETTIME: | ||
1401 | if (seq_mode == SEQ_2) | ||
1402 | return tmr->ioctl(tmr_no, cmd, arg); | ||
1403 | val = jiffies - seq_time; | ||
1404 | break; | ||
1405 | |||
1406 | case SNDCTL_SEQ_CTRLRATE: | ||
1407 | /* | ||
1408 | * If *arg == 0, just return the current rate | ||
1409 | */ | ||
1410 | if (seq_mode == SEQ_2) | ||
1411 | return tmr->ioctl(tmr_no, cmd, arg); | ||
1412 | |||
1413 | if (get_user(val, p)) | ||
1414 | return -EFAULT; | ||
1415 | if (val != 0) | ||
1416 | return -EINVAL; | ||
1417 | val = HZ; | ||
1418 | break; | ||
1419 | |||
1420 | case SNDCTL_SEQ_RESETSAMPLES: | ||
1421 | case SNDCTL_SYNTH_REMOVESAMPLE: | ||
1422 | case SNDCTL_SYNTH_CONTROL: | ||
1423 | if (get_user(dev, p)) | ||
1424 | return -EFAULT; | ||
1425 | if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL) | ||
1426 | return -ENXIO; | ||
1427 | if (!(synth_open_mask & (1 << dev)) && !orig_dev) | ||
1428 | return -EBUSY; | ||
1429 | return synth_devs[dev]->ioctl(dev, cmd, arg); | ||
1430 | |||
1431 | case SNDCTL_SEQ_NRSYNTHS: | ||
1432 | val = max_synthdev; | ||
1433 | break; | ||
1434 | |||
1435 | case SNDCTL_SEQ_NRMIDIS: | ||
1436 | val = max_mididev; | ||
1437 | break; | ||
1438 | |||
1439 | case SNDCTL_SYNTH_MEMAVL: | ||
1440 | if (get_user(dev, p)) | ||
1441 | return -EFAULT; | ||
1442 | if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL) | ||
1443 | return -ENXIO; | ||
1444 | if (!(synth_open_mask & (1 << dev)) && !orig_dev) | ||
1445 | return -EBUSY; | ||
1446 | val = synth_devs[dev]->ioctl(dev, cmd, arg); | ||
1447 | break; | ||
1448 | |||
1449 | case SNDCTL_FM_4OP_ENABLE: | ||
1450 | if (get_user(dev, p)) | ||
1451 | return -EFAULT; | ||
1452 | if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL) | ||
1453 | return -ENXIO; | ||
1454 | if (!(synth_open_mask & (1 << dev))) | ||
1455 | return -ENXIO; | ||
1456 | synth_devs[dev]->ioctl(dev, cmd, arg); | ||
1457 | return 0; | ||
1458 | |||
1459 | case SNDCTL_SYNTH_INFO: | ||
1460 | if (get_user(dev, &((struct synth_info __user *)arg)->device)) | ||
1461 | return -EFAULT; | ||
1462 | if (dev < 0 || dev >= max_synthdev) | ||
1463 | return -ENXIO; | ||
1464 | if (!(synth_open_mask & (1 << dev)) && !orig_dev) | ||
1465 | return -EBUSY; | ||
1466 | return synth_devs[dev]->ioctl(dev, cmd, arg); | ||
1467 | |||
1468 | /* Like SYNTH_INFO but returns ID in the name field */ | ||
1469 | case SNDCTL_SYNTH_ID: | ||
1470 | if (get_user(dev, &((struct synth_info __user *)arg)->device)) | ||
1471 | return -EFAULT; | ||
1472 | if (dev < 0 || dev >= max_synthdev) | ||
1473 | return -ENXIO; | ||
1474 | if (!(synth_open_mask & (1 << dev)) && !orig_dev) | ||
1475 | return -EBUSY; | ||
1476 | memcpy(&inf, synth_devs[dev]->info, sizeof(inf)); | ||
1477 | strlcpy(inf.name, synth_devs[dev]->id, sizeof(inf.name)); | ||
1478 | inf.device = dev; | ||
1479 | return copy_to_user(arg, &inf, sizeof(inf))?-EFAULT:0; | ||
1480 | |||
1481 | case SNDCTL_SEQ_OUTOFBAND: | ||
1482 | if (copy_from_user(&event_rec, arg, sizeof(event_rec))) | ||
1483 | return -EFAULT; | ||
1484 | spin_lock_irqsave(&lock,flags); | ||
1485 | play_event(event_rec.arr); | ||
1486 | spin_unlock_irqrestore(&lock,flags); | ||
1487 | return 0; | ||
1488 | |||
1489 | case SNDCTL_MIDI_INFO: | ||
1490 | if (get_user(dev, &((struct midi_info __user *)arg)->device)) | ||
1491 | return -EFAULT; | ||
1492 | if (dev < 0 || dev >= max_mididev || !midi_devs[dev]) | ||
1493 | return -ENXIO; | ||
1494 | midi_devs[dev]->info.device = dev; | ||
1495 | return copy_to_user(arg, &midi_devs[dev]->info, sizeof(struct midi_info))?-EFAULT:0; | ||
1496 | |||
1497 | case SNDCTL_SEQ_THRESHOLD: | ||
1498 | if (get_user(val, p)) | ||
1499 | return -EFAULT; | ||
1500 | if (val < 1) | ||
1501 | val = 1; | ||
1502 | if (val >= SEQ_MAX_QUEUE) | ||
1503 | val = SEQ_MAX_QUEUE - 1; | ||
1504 | output_threshold = val; | ||
1505 | return 0; | ||
1506 | |||
1507 | case SNDCTL_MIDI_PRETIME: | ||
1508 | if (get_user(val, p)) | ||
1509 | return -EFAULT; | ||
1510 | if (val < 0) | ||
1511 | val = 0; | ||
1512 | val = (HZ * val) / 10; | ||
1513 | pre_event_timeout = val; | ||
1514 | break; | ||
1515 | |||
1516 | default: | ||
1517 | if (mode == OPEN_READ) | ||
1518 | return -EIO; | ||
1519 | if (!synth_devs[0]) | ||
1520 | return -ENXIO; | ||
1521 | if (!(synth_open_mask & (1 << 0))) | ||
1522 | return -ENXIO; | ||
1523 | if (!synth_devs[0]->ioctl) | ||
1524 | return -EINVAL; | ||
1525 | return synth_devs[0]->ioctl(0, cmd, arg); | ||
1526 | } | ||
1527 | return put_user(val, p); | ||
1528 | } | ||
1529 | |||
1530 | /* No kernel lock - we're using the global irq lock here */ | ||
1531 | unsigned int sequencer_poll(int dev, struct file *file, poll_table * wait) | ||
1532 | { | ||
1533 | unsigned long flags; | ||
1534 | unsigned int mask = 0; | ||
1535 | |||
1536 | dev = dev >> 4; | ||
1537 | |||
1538 | spin_lock_irqsave(&lock,flags); | ||
1539 | /* input */ | ||
1540 | poll_wait(file, &midi_sleeper, wait); | ||
1541 | if (iqlen) | ||
1542 | mask |= POLLIN | POLLRDNORM; | ||
1543 | |||
1544 | /* output */ | ||
1545 | poll_wait(file, &seq_sleeper, wait); | ||
1546 | if ((SEQ_MAX_QUEUE - qlen) >= output_threshold) | ||
1547 | mask |= POLLOUT | POLLWRNORM; | ||
1548 | spin_unlock_irqrestore(&lock,flags); | ||
1549 | return mask; | ||
1550 | } | ||
1551 | |||
1552 | |||
1553 | void sequencer_timer(unsigned long dummy) | ||
1554 | { | ||
1555 | seq_startplay(); | ||
1556 | } | ||
1557 | |||
1558 | int note_to_freq(int note_num) | ||
1559 | { | ||
1560 | |||
1561 | /* | ||
1562 | * This routine converts a midi note to a frequency (multiplied by 1000) | ||
1563 | */ | ||
1564 | |||
1565 | int note, octave, note_freq; | ||
1566 | static int notes[] = | ||
1567 | { | ||
1568 | 261632, 277189, 293671, 311132, 329632, 349232, | ||
1569 | 369998, 391998, 415306, 440000, 466162, 493880 | ||
1570 | }; | ||
1571 | |||
1572 | #define BASE_OCTAVE 5 | ||
1573 | |||
1574 | octave = note_num / 12; | ||
1575 | note = note_num % 12; | ||
1576 | |||
1577 | note_freq = notes[note]; | ||
1578 | |||
1579 | if (octave < BASE_OCTAVE) | ||
1580 | note_freq >>= (BASE_OCTAVE - octave); | ||
1581 | else if (octave > BASE_OCTAVE) | ||
1582 | note_freq <<= (octave - BASE_OCTAVE); | ||
1583 | |||
1584 | /* | ||
1585 | * note_freq >>= 1; | ||
1586 | */ | ||
1587 | |||
1588 | return note_freq; | ||
1589 | } | ||
1590 | |||
1591 | unsigned long compute_finetune(unsigned long base_freq, int bend, int range, | ||
1592 | int vibrato_cents) | ||
1593 | { | ||
1594 | unsigned long amount; | ||
1595 | int negative, semitones, cents, multiplier = 1; | ||
1596 | |||
1597 | if (!bend) | ||
1598 | return base_freq; | ||
1599 | if (!range) | ||
1600 | return base_freq; | ||
1601 | |||
1602 | if (!base_freq) | ||
1603 | return base_freq; | ||
1604 | |||
1605 | if (range >= 8192) | ||
1606 | range = 8192; | ||
1607 | |||
1608 | bend = bend * range / 8192; /* Convert to cents */ | ||
1609 | bend += vibrato_cents; | ||
1610 | |||
1611 | if (!bend) | ||
1612 | return base_freq; | ||
1613 | |||
1614 | negative = bend < 0 ? 1 : 0; | ||
1615 | |||
1616 | if (bend < 0) | ||
1617 | bend *= -1; | ||
1618 | if (bend > range) | ||
1619 | bend = range; | ||
1620 | |||
1621 | /* | ||
1622 | if (bend > 2399) | ||
1623 | bend = 2399; | ||
1624 | */ | ||
1625 | while (bend > 2399) | ||
1626 | { | ||
1627 | multiplier *= 4; | ||
1628 | bend -= 2400; | ||
1629 | } | ||
1630 | |||
1631 | semitones = bend / 100; | ||
1632 | if (semitones > 99) | ||
1633 | semitones = 99; | ||
1634 | cents = bend % 100; | ||
1635 | |||
1636 | amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000; | ||
1637 | |||
1638 | if (negative) | ||
1639 | return (base_freq * 10000) / amount; /* Bend down */ | ||
1640 | else | ||
1641 | return (base_freq * amount) / 10000; /* Bend up */ | ||
1642 | } | ||
1643 | |||
1644 | |||
1645 | void sequencer_init(void) | ||
1646 | { | ||
1647 | /* drag in sequencer_syms.o */ | ||
1648 | { | ||
1649 | extern char sequencer_syms_symbol; | ||
1650 | sequencer_syms_symbol = 0; | ||
1651 | } | ||
1652 | |||
1653 | if (sequencer_ok) | ||
1654 | return; | ||
1655 | MIDIbuf_init(); | ||
1656 | queue = (unsigned char *)vmalloc(SEQ_MAX_QUEUE * EV_SZ); | ||
1657 | if (queue == NULL) | ||
1658 | { | ||
1659 | printk(KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n"); | ||
1660 | return; | ||
1661 | } | ||
1662 | iqueue = (unsigned char *)vmalloc(SEQ_MAX_QUEUE * IEV_SZ); | ||
1663 | if (iqueue == NULL) | ||
1664 | { | ||
1665 | printk(KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n"); | ||
1666 | vfree(queue); | ||
1667 | return; | ||
1668 | } | ||
1669 | sequencer_ok = 1; | ||
1670 | } | ||
1671 | |||
1672 | void sequencer_unload(void) | ||
1673 | { | ||
1674 | if(queue) | ||
1675 | { | ||
1676 | vfree(queue); | ||
1677 | queue=NULL; | ||
1678 | } | ||
1679 | if(iqueue) | ||
1680 | { | ||
1681 | vfree(iqueue); | ||
1682 | iqueue=NULL; | ||
1683 | } | ||
1684 | } | ||