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