diff options
Diffstat (limited to 'sys/i386/isa/sound/sound_timer.c')
-rw-r--r-- | sys/i386/isa/sound/sound_timer.c | 458 |
1 files changed, 198 insertions, 260 deletions
diff --git a/sys/i386/isa/sound/sound_timer.c b/sys/i386/isa/sound/sound_timer.c index c6544c82a7ee..8450b2d73503 100644 --- a/sys/i386/isa/sound/sound_timer.c +++ b/sys/i386/isa/sound/sound_timer.c @@ -1,11 +1,8 @@ /* * sound/sound_timer.c - * - * Timer for the level 2 interface of the /dev/sequencer. Uses the - * 80 and 320 usec timers of OPL-3 (PAS16 only) and GUS. - * + * * Copyright by Hannu Savolainen 1993 - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: 1. Redistributions of source code must retain the above copyright @@ -13,7 +10,7 @@ * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -25,381 +22,322 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * */ #define SEQUENCER_C -#include "sound_config.h" +#include <i386/isa/sound/sound_config.h> -#ifdef CONFIGURE_SOUNDCARD +#if NSND > 0 -#if !defined(EXCLUDE_SEQUENCER) && (!defined(EXCLUDE_GUS) || (!defined(EXCLUDE_PAS) && !defined(EXCLUDE_YM3812))) +#if defined(CONFIG_SEQUENCER) static volatile int initialized = 0, opened = 0, tmr_running = 0; static volatile time_t tmr_offs, tmr_ctr; -static volatile unsigned long ticks_offs; +static volatile u_long ticks_offs; static volatile int curr_tempo, curr_timebase; -static volatile unsigned long curr_ticks; -static volatile unsigned long next_event_time; -static unsigned long prev_event_time; -static volatile int select_addr, data_addr; -static volatile int curr_timer = 0; -static volatile unsigned long usecs_per_tmr; /* Length of the current interval */ +static volatile u_long curr_ticks; +static volatile u_long next_event_time; +static u_long prev_event_time; +static volatile u_long usecs_per_tmr; /* Length of the current interval */ +static struct sound_lowlev_timer *tmr = NULL; -static void -timer_command (unsigned int addr, unsigned int val) +static u_long +tmr2ticks(int tmr_value) { - int i; + /* + * Convert timer ticks to MIDI ticks + */ - OUTB ((unsigned char) (addr & 0xff), select_addr); + u_long tmp; + u_long scale; - for (i = 0; i < 2; i++) - INB (select_addr); + tmp = tmr_value * usecs_per_tmr; /* Convert to usecs */ - OUTB ((unsigned char) (val & 0xff), data_addr); + scale = (60 * 1000000) / (curr_tempo * curr_timebase); /* usecs per MIDI tick */ - for (i = 0; i < 2; i++) - INB (select_addr); + return (tmp + (scale / 2)) / scale; } static void -arm_timer (int timer, unsigned int interval) -{ - - curr_timer = timer; - - if (timer == 1) - { - gus_write8 (0x46, 256 - interval); /* Set counter for timer 1 */ - gus_write8 (0x45, 0x04); /* Enable timer 1 IRQ */ - timer_command (0x04, 0x01); /* Start timer 1 */ - } - else - { - gus_write8 (0x47, 256 - interval); /* Set counter for timer 2 */ - gus_write8 (0x45, 0x08); /* Enable timer 2 IRQ */ - timer_command (0x04, 0x02); /* Start timer 2 */ - } -} - -static unsigned long -tmr2ticks (int tmr_value) +reprogram_timer(void) { - /* - * Convert timer ticks to MIDI ticks - */ - - unsigned long tmp; - unsigned long scale; + u_long usecs_per_tick; - tmp = tmr_value * usecs_per_tmr; /* Convert to usecs */ + usecs_per_tick = (60 * 1000000) / (curr_tempo * curr_timebase); - scale = (60 * 1000000) / (curr_tempo * curr_timebase); /* usecs per MIDI tick */ + /* + * Don't kill the system by setting too high timer rate + */ + if (usecs_per_tick < 2000) + usecs_per_tick = 2000; - return (tmp + (scale / 2)) / scale; + usecs_per_tmr = tmr->tmr_start(tmr->dev, usecs_per_tick); } -static void -reprogram_timer (void) +void +sound_timer_syncinterval(u_int new_usecs) { - unsigned long usecs_per_tick; - int timer_no, resolution; - int divisor; - - usecs_per_tick = (60 * 1000000) / (curr_tempo * curr_timebase); - - /* - * Don't kill the system by setting too high timer rate - */ - if (usecs_per_tick < 2000) - usecs_per_tick = 2000; - - if (usecs_per_tick > (256 * 80)) - { - timer_no = 2; - resolution = 320; /* usec */ - } - else - { - timer_no = 1; - resolution = 80; /* usec */ - } - - divisor = (usecs_per_tick + (resolution / 2)) / resolution; - usecs_per_tmr = divisor * resolution; - - arm_timer (timer_no, divisor); + /* + * This routine is called by the hardware level if the clock + * frequency has changed for some reason. + */ + tmr_offs = tmr_ctr; + ticks_offs += tmr2ticks(tmr_ctr); + tmr_ctr = 0; + + usecs_per_tmr = new_usecs; } static void -tmr_reset (void) +tmr_reset(void) { - unsigned long flags; - - DISABLE_INTR (flags); - tmr_offs = 0; - ticks_offs = 0; - tmr_ctr = 0; - next_event_time = 0xffffffff; - prev_event_time = 0; - curr_ticks = 0; - RESTORE_INTR (flags); + u_long flags; + + flags = splhigh(); + tmr_offs = 0; + ticks_offs = 0; + tmr_ctr = 0; + next_event_time = 0xffffffff; + prev_event_time = 0; + curr_ticks = 0; + splx(flags); } static int -timer_open (int dev, int mode) +timer_open(int dev, int mode) { - if (opened) - return RET_ERROR (EBUSY); + if (opened) + return -(EBUSY); - tmr_reset (); - curr_tempo = 60; - curr_timebase = HZ; - opened = 1; - reprogram_timer (); + tmr_reset(); + curr_tempo = 60; + curr_timebase = hz; + opened = 1; + reprogram_timer(); - return 0; + return 0; } static void -timer_close (int dev) +timer_close(int dev) { - opened = tmr_running = 0; - gus_write8 (0x45, 0); /* Disable both timers */ + opened = tmr_running = 0; + tmr->tmr_disable(tmr->dev); } static int -timer_event (int dev, unsigned char *event) +timer_event(int dev, u_char *event) { - unsigned char cmd = event[1]; - unsigned long parm = *(int *) &event[4]; + u_char cmd = event[1]; + u_long parm = *(int *) &event[4]; - switch (cmd) - { + switch (cmd) { case TMR_WAIT_REL: - parm += prev_event_time; + parm += prev_event_time; case TMR_WAIT_ABS: - if (parm > 0) - { - long time; + if (parm > 0) { + long time; - if (parm <= curr_ticks) /* It's the time */ - return TIMER_NOT_ARMED; + if (parm <= curr_ticks) /* It's the time */ + return TIMER_NOT_ARMED; - time = parm; - next_event_time = prev_event_time = time; + time = parm; + next_event_time = prev_event_time = time; - return TIMER_ARMED; + return TIMER_ARMED; } - break; + break; case TMR_START: - tmr_reset (); - tmr_running = 1; - reprogram_timer (); - break; + tmr_reset(); + tmr_running = 1; + reprogram_timer(); + break; case TMR_STOP: - tmr_running = 0; - break; + tmr_running = 0; + break; case TMR_CONTINUE: - tmr_running = 1; - reprogram_timer (); - break; + tmr_running = 1; + reprogram_timer(); + break; case TMR_TEMPO: - if (parm) - { - if (parm < 8) - parm = 8; - if (parm > 250) - parm = 250; - tmr_offs = tmr_ctr; - ticks_offs += tmr2ticks (tmr_ctr); - tmr_ctr = 0; - curr_tempo = parm; - reprogram_timer (); + if (parm) { + if (parm < 8) + parm = 8; + if (parm > 250) + parm = 250; + tmr_offs = tmr_ctr; + ticks_offs += tmr2ticks(tmr_ctr); + tmr_ctr = 0; + curr_tempo = parm; + reprogram_timer(); } - break; + break; case TMR_ECHO: - seq_copy_to_input (event, 8); - break; + seq_copy_to_input(event, 8); + break; default:; } - return TIMER_NOT_ARMED; + return TIMER_NOT_ARMED; } -static unsigned long -timer_get_time (int dev) +static u_long +timer_get_time(int dev) { - if (!opened) - return 0; + if (!opened) + return 0; - return curr_ticks; + return curr_ticks; } static int -timer_ioctl (int dev, - unsigned int cmd, unsigned int arg) +timer_ioctl(int dev, u_int cmd, ioctl_arg arg) { - switch (cmd) - { + switch (cmd) { case SNDCTL_TMR_SOURCE: - return IOCTL_OUT (arg, TMR_INTERNAL); - break; + return *(int *) arg = TMR_INTERNAL; + break; case SNDCTL_TMR_START: - tmr_reset (); - tmr_running = 1; - return 0; - break; + tmr_reset(); + tmr_running = 1; + return 0; + break; case SNDCTL_TMR_STOP: - tmr_running = 0; - return 0; - break; + tmr_running = 0; + return 0; + break; case SNDCTL_TMR_CONTINUE: - tmr_running = 1; - return 0; - break; + tmr_running = 1; + return 0; + break; case SNDCTL_TMR_TIMEBASE: - { - int val = IOCTL_IN (arg); - - if (val) - { - if (val < 1) - val = 1; - if (val > 1000) - val = 1000; - curr_timebase = val; - } - - return IOCTL_OUT (arg, curr_timebase); - } - break; + { + int val = (*(int *) arg); + + if (val) { + if (val < 1) + val = 1; + if (val > 1000) + val = 1000; + curr_timebase = val; + } + return *(int *) arg = curr_timebase; + } + break; case SNDCTL_TMR_TEMPO: - { - int val = IOCTL_IN (arg); - - if (val) - { - if (val < 8) - val = 8; - if (val > 250) - val = 250; - tmr_offs = tmr_ctr; - ticks_offs += tmr2ticks (tmr_ctr); - tmr_ctr = 0; - curr_tempo = val; - reprogram_timer (); - } - - return IOCTL_OUT (arg, curr_tempo); - } - break; + { + int val = (*(int *) arg); + + if (val) { + if (val < 8) + val = 8; + if (val > 250) + val = 250; + tmr_offs = tmr_ctr; + ticks_offs += tmr2ticks(tmr_ctr); + tmr_ctr = 0; + curr_tempo = val; + reprogram_timer(); + } + return *(int *) arg = curr_tempo; + } + break; case SNDCTL_SEQ_CTRLRATE: - if (IOCTL_IN (arg) != 0) /* Can't change */ - return RET_ERROR (EINVAL); + if ((*(int *) arg) != 0) /* Can't change */ + return -(EINVAL); - return IOCTL_OUT (arg, ((curr_tempo * curr_timebase) + 30) / 60); - break; + return *(int *) arg = ((curr_tempo * curr_timebase) + 30) / 60; + break; case SNDCTL_TMR_METRONOME: - /* NOP */ - break; + /* NOP */ + break; - default: + default:; } - return RET_ERROR (EINVAL); + return -(EINVAL); } static void -timer_arm (int dev, long time) +timer_arm(int dev, long time) { - if (time < 0) - time = curr_ticks + 1; - else if (time <= curr_ticks) /* It's the time */ - return; + if (time < 0) + time = curr_ticks + 1; + else if (time <= curr_ticks) /* It's the time */ + return; - next_event_time = prev_event_time = time; + next_event_time = prev_event_time = time; - return; + return; } static struct sound_timer_operations sound_timer = { - {"OPL-3/GUS Timer", 0}, - 1, /* Priority */ - 0, /* Local device link */ - timer_open, - timer_close, - timer_event, - timer_get_time, - timer_ioctl, - timer_arm + {"GUS Timer", 0}, + 1, /* Priority */ + 0, /* Local device link */ + timer_open, + timer_close, + timer_event, + timer_get_time, + timer_ioctl, + timer_arm }; void -sound_timer_interrupt (void) +sound_timer_interrupt(void) { - gus_write8 (0x45, 0); /* Ack IRQ */ - timer_command (4, 0x80); /* Reset IRQ flags */ + if (!opened) + return; - if (!opened) - return; + tmr->tmr_restart(tmr->dev); - if (curr_timer == 1) - gus_write8 (0x45, 0x04); /* Start timer 1 again */ - else - gus_write8 (0x45, 0x08); /* Start timer 2 again */ + if (!tmr_running) + return; - if (!tmr_running) - return; + tmr_ctr++; + curr_ticks = ticks_offs + tmr2ticks(tmr_ctr); - tmr_ctr++; - curr_ticks = ticks_offs + tmr2ticks (tmr_ctr); - - if (curr_ticks >= next_event_time) - { - next_event_time = 0xffffffff; - sequencer_timer (); + if (curr_ticks >= next_event_time) { + next_event_time = 0xffffffff; + sequencer_timer(0); } } void -sound_timer_init (int io_base) +sound_timer_init(struct sound_lowlev_timer * t, char *name) { - int n; + int n; - if (initialized) - return; /* There is already a similar timer */ + if (initialized || t == NULL) + return; /* There is already a similar timer */ - select_addr = io_base; - data_addr = io_base + 1; + initialized = 1; + tmr = t; - initialized = 1; + if (num_sound_timers >= MAX_TIMER_DEV) + n = 0; /* Overwrite the system timer */ + else + n = num_sound_timers++; -#if 1 - if (num_sound_timers >= MAX_TIMER_DEV) - n = 0; /* Overwrite the system timer */ - else - n = num_sound_timers++; -#else - n = 0; -#endif + strcpy(sound_timer.info.name, name); - sound_timer_devs[n] = &sound_timer; + sound_timer_devs[n] = &sound_timer; } #endif |