root/drivers/sound/sys_timer.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tmr2ticks
  2. poll_def_tmr
  3. tmr_reset
  4. def_tmr_open
  5. def_tmr_close
  6. def_tmr_event
  7. def_tmr_get_time
  8. def_tmr_ioctl
  9. def_tmr_arm

   1 /*
   2  * sound/sys_timer.c
   3  *
   4  * The default timer for the Level 2 sequencer interface
   5  * Uses the (100HZ) timer of kernel.
   6  *
   7  * Copyright by Hannu Savolainen 1993
   8  *
   9  * Redistribution and use in source and binary forms, with or without
  10  * modification, are permitted provided that the following conditions are
  11  * met: 1. Redistributions of source code must retain the above copyright
  12  * notice, this list of conditions and the following disclaimer. 2.
  13  * Redistributions in binary form must reproduce the above copyright notice,
  14  * this list of conditions and the following disclaimer in the documentation
  15  * and/or other materials provided with the distribution.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  *
  29  */
  30 
  31 #define SEQUENCER_C
  32 #include "sound_config.h"
  33 
  34 #ifdef CONFIG_SEQUENCER
  35 
  36 static volatile int opened = 0, tmr_running = 0;
  37 static volatile time_t tmr_offs, tmr_ctr;
  38 static volatile unsigned long ticks_offs;
  39 static volatile int curr_tempo, curr_timebase;
  40 static volatile unsigned long curr_ticks;
  41 static volatile unsigned long next_event_time;
  42 static unsigned long prev_event_time;
  43 
  44 static void     poll_def_tmr (unsigned long dummy);
  45 
  46 
  47 static struct timer_list def_tmr =
  48 {NULL, NULL, 0, 0, poll_def_tmr};
  49 
  50 static unsigned long
  51 tmr2ticks (int tmr_value)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53   /*
  54    *    Convert system timer ticks (HZ) to MIDI ticks
  55    *    (divide # of MIDI ticks/minute by # of system ticks/minute).
  56    */
  57 
  58   return ((tmr_value * curr_tempo * curr_timebase) + (30 * HZ)) / (60 * HZ);
  59 }
  60 
  61 static void
  62 poll_def_tmr (unsigned long dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64 
  65   if (opened)
  66     {
  67 
  68       {
  69         def_tmr.expires = (1) + jiffies;
  70         add_timer (&def_tmr);
  71       };
  72 
  73       if (tmr_running)
  74         {
  75           tmr_ctr++;
  76           curr_ticks = ticks_offs + tmr2ticks (tmr_ctr);
  77 
  78           if (curr_ticks >= next_event_time)
  79             {
  80               next_event_time = 0xffffffff;
  81               sequencer_timer (0);
  82             }
  83         }
  84     }
  85 }
  86 
  87 static void
  88 tmr_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90   unsigned long   flags;
  91 
  92   save_flags (flags);
  93   cli ();
  94   tmr_offs = 0;
  95   ticks_offs = 0;
  96   tmr_ctr = 0;
  97   next_event_time = 0xffffffff;
  98   prev_event_time = 0;
  99   curr_ticks = 0;
 100   restore_flags (flags);
 101 }
 102 
 103 static int
 104 def_tmr_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106   if (opened)
 107     return -EBUSY;
 108 
 109   tmr_reset ();
 110   curr_tempo = 60;
 111   curr_timebase = HZ;
 112   opened = 1;
 113 
 114   ;
 115 
 116   {
 117     def_tmr.expires = (1) + jiffies;
 118     add_timer (&def_tmr);
 119   };
 120 
 121   return 0;
 122 }
 123 
 124 static void
 125 def_tmr_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127   opened = tmr_running = 0;
 128   del_timer (&def_tmr);;
 129 }
 130 
 131 static int
 132 def_tmr_event (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134   unsigned char   cmd = event[1];
 135   unsigned long   parm = *(int *) &event[4];
 136 
 137   switch (cmd)
 138     {
 139     case TMR_WAIT_REL:
 140       parm += prev_event_time;
 141     case TMR_WAIT_ABS:
 142       if (parm > 0)
 143         {
 144           long            time;
 145 
 146           if (parm <= curr_ticks)       /* It's the time */
 147             return TIMER_NOT_ARMED;
 148 
 149           time = parm;
 150           next_event_time = prev_event_time = time;
 151 
 152           return TIMER_ARMED;
 153         }
 154       break;
 155 
 156     case TMR_START:
 157       tmr_reset ();
 158       tmr_running = 1;
 159       break;
 160 
 161     case TMR_STOP:
 162       tmr_running = 0;
 163       break;
 164 
 165     case TMR_CONTINUE:
 166       tmr_running = 1;
 167       break;
 168 
 169     case TMR_TEMPO:
 170       if (parm)
 171         {
 172           if (parm < 8)
 173             parm = 8;
 174           if (parm > 360)
 175             parm = 360;
 176           tmr_offs = tmr_ctr;
 177           ticks_offs += tmr2ticks (tmr_ctr);
 178           tmr_ctr = 0;
 179           curr_tempo = parm;
 180         }
 181       break;
 182 
 183     case TMR_ECHO:
 184       seq_copy_to_input (event, 8);
 185       break;
 186 
 187     default:;
 188     }
 189 
 190   return TIMER_NOT_ARMED;
 191 }
 192 
 193 static unsigned long
 194 def_tmr_get_time (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196   if (!opened)
 197     return 0;
 198 
 199   return curr_ticks;
 200 }
 201 
 202 static int
 203 def_tmr_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 204                unsigned int cmd, ioctl_arg arg)
 205 {
 206   switch (cmd)
 207     {
 208     case SNDCTL_TMR_SOURCE:
 209       return snd_ioctl_return ((int *) arg, TMR_INTERNAL);
 210       break;
 211 
 212     case SNDCTL_TMR_START:
 213       tmr_reset ();
 214       tmr_running = 1;
 215       return 0;
 216       break;
 217 
 218     case SNDCTL_TMR_STOP:
 219       tmr_running = 0;
 220       return 0;
 221       break;
 222 
 223     case SNDCTL_TMR_CONTINUE:
 224       tmr_running = 1;
 225       return 0;
 226       break;
 227 
 228     case SNDCTL_TMR_TIMEBASE:
 229       {
 230         int             val = get_fs_long ((long *) arg);
 231 
 232         if (val)
 233           {
 234             if (val < 1)
 235               val = 1;
 236             if (val > 1000)
 237               val = 1000;
 238             curr_timebase = val;
 239           }
 240 
 241         return snd_ioctl_return ((int *) arg, curr_timebase);
 242       }
 243       break;
 244 
 245     case SNDCTL_TMR_TEMPO:
 246       {
 247         int             val = get_fs_long ((long *) arg);
 248 
 249         if (val)
 250           {
 251             if (val < 8)
 252               val = 8;
 253             if (val > 250)
 254               val = 250;
 255             tmr_offs = tmr_ctr;
 256             ticks_offs += tmr2ticks (tmr_ctr);
 257             tmr_ctr = 0;
 258             curr_tempo = val;
 259           }
 260 
 261         return snd_ioctl_return ((int *) arg, curr_tempo);
 262       }
 263       break;
 264 
 265     case SNDCTL_SEQ_CTRLRATE:
 266       if (get_fs_long ((long *) arg) != 0)      /* Can't change */
 267         return -EINVAL;
 268 
 269       return snd_ioctl_return ((int *) arg, ((curr_tempo * curr_timebase) + 30) / 60);
 270       break;
 271 
 272     case SNDCTL_TMR_METRONOME:
 273       /* NOP */
 274       break;
 275 
 276     default:;
 277     }
 278 
 279   return -EINVAL;
 280 }
 281 
 282 static void
 283 def_tmr_arm (int dev, long time)
     /* [previous][next][first][last][top][bottom][index][help] */
 284 {
 285   if (time < 0)
 286     time = curr_ticks + 1;
 287   else if (time <= curr_ticks)  /* It's the time */
 288     return;
 289 
 290   next_event_time = prev_event_time = time;
 291 
 292   return;
 293 }
 294 
 295 struct sound_timer_operations default_sound_timer =
 296 {
 297   {"System clock", 0},
 298   0,                            /* Priority */
 299   0,                            /* Local device link */
 300   def_tmr_open,
 301   def_tmr_close,
 302   def_tmr_event,
 303   def_tmr_get_time,
 304   def_tmr_ioctl,
 305   def_tmr_arm
 306 };
 307 
 308 #endif

/* [previous][next][first][last][top][bottom][index][help] */