File: timer.c

package info (click to toggle)
alsadriver 0.2.0-pre8-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,808 kB
  • ctags: 6,550
  • sloc: ansic: 43,490; sh: 916; makefile: 759; perl: 54
file content (383 lines) | stat: -rw-r--r-- 10,438 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 *  Timers abstract layer
 *  Copyright (c) by Jaroslav Kysela <perex@jcu.cz>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define SND_MAIN_OBJECT_FILE
#include "driver.h"
#include "timer.h"
#include "info.h"

snd_timer_t *snd_timer_devices[ SND_TIMER_DEVICES ];

snd_mutex_define_static( register );
snd_mutex_define_static( open );

/*
 *
 */

snd_timer_t *snd_timer_open( char *owner, unsigned int resolution )
{
  int idx;
  snd_timer_t *timer;
  
  snd_mutex_down_static( open );
  for ( idx = SND_TIMER_DEVICES - 1; idx >= 0; idx-- ) {
    timer = snd_timer_devices[ idx ];
    if ( timer == NULL ) continue;
    if ( !(timer -> flags & SND_TIMER_FLG_USED) &&
         timer -> hw.resolution <= resolution ) {
      if ( timer -> hw.open ) {
        if ( !timer -> hw.open( timer ) ) break;
      } else {
        break;
      }
    }
    timer = NULL;
  }
  if ( timer == NULL ) {
    for ( idx = 0; idx < SND_TIMER_DEVICES; idx++ ) {
      timer = snd_timer_devices[ idx ];
      if ( timer == NULL ) continue;
      if ( !(timer -> flags & SND_TIMER_FLG_USED) ) {
        if ( timer -> hw.open ) {
          if ( !timer -> hw.open( timer ) ) break;
        } else {
          break;
        }
      }
      timer = NULL;
    }
    if ( timer == NULL ) {
      snd_mutex_up_static( open );
      return NULL;
    }
  }
  timer -> flags |= SND_TIMER_FLG_USED;
  timer -> owner = snd_malloc_strdup( owner );
  MOD_INC_USE_COUNT;
  timer -> card -> use_inc( timer -> card );
  snd_mutex_up_static( open );
  return timer;
}

int snd_timer_close( snd_timer_t *timer )
{
  if ( !timer ) return -EINVAL;
  snd_mutex_down_static( open );
  if ( timer -> hw.close )
    timer -> hw.close( timer );
  timer -> flags &= ~SND_TIMER_FLG_USED;
  snd_free_str( timer -> owner );
  timer -> owner = NULL;
  MOD_DEC_USE_COUNT;
  snd_mutex_up_static( open );
  timer -> card -> use_dec( timer -> card );
  return 0;
}

unsigned snd_timer_resolution( snd_timer_t *timer )
{
  if ( timer -> hw.c_resolution )
    return timer -> hw.c_resolution( timer );
  return timer -> hw.resolution;
}

void snd_timer_start( snd_timer_t *timer, unsigned int ticks )
{
  if ( !timer ) return;
  if ( timer -> hw.low_ticks > ticks ) ticks = timer -> hw.low_ticks;
  timer -> ticks = timer -> cticks = ticks;
  timer -> hw.start( timer );
}

void snd_timer_stop( snd_timer_t *timer )
{
  if ( !timer ) return;
  timer -> hw.stop( timer );
}

void snd_timer_continue( snd_timer_t *timer )
{
  if ( timer -> hw.t_continue ) {
    timer -> hw.t_continue( timer );
    return;
  }
  if ( !timer -> cticks ) timer -> cticks = 1;
  timer -> hw.start( timer );
}

snd_timer_t *snd_timer_open_always( char *owner, unsigned int resolution )
{
  snd_timer_t *timer;
  
  timer = snd_timer_open( owner, resolution );
  if ( timer != NULL ) return timer;
  return snd_timer_open_system( owner );
}

int snd_timer_close_always( snd_timer_t *timer )
{
  if ( timer -> flags & SND_TIMER_FLG_SYSTEM )
    return snd_timer_close_system( timer );
  return snd_timer_close( timer );
}

/*
 *
 */

snd_timer_t *snd_timer_new_device( snd_card_t *card, char *id )
{
  snd_timer_t *timer;

  timer = (snd_timer_t *)snd_calloc( sizeof( snd_timer_t ) );
  if ( !timer ) return NULL;
  timer -> card = card;
  if ( id ) {
    strncpy( timer -> id, id, sizeof( timer -> id ) - 1 );
  }
  return timer;
}

int snd_timer_free( snd_timer_t *timer )
{
  if ( !timer ) return -EINVAL;
  if ( timer -> private_free )
    timer -> private_free( timer -> private_data );
  snd_free( timer, sizeof( snd_timer_t ) );
  return 0;  
}

int snd_timer_register( snd_timer_t *timer )
{
  int idx, idx1;
  snd_timer_t *timer1;

  if ( !timer ) return -EINVAL;
  if ( snd_timer_devices[ SND_TIMER_DEVICES - 1 ] != NULL ) return -EBUSY;
  snd_mutex_down_static( register );
  for ( idx = 0; idx < SND_TIMER_DEVICES; idx++ ) {
    if ( (timer1 = snd_timer_devices[ idx ]) == NULL ) {
      snd_timer_devices[ idx ] = timer;
      break;
    }
    if ( timer1 -> hw.resolution > timer -> hw.resolution ) {
      for ( idx1 = SND_TIMER_DEVICES - 1; idx1 > idx; idx1-- ) {
        snd_timer_devices[ idx1 ] = snd_timer_devices[ idx1 - 1 ];
        if ( snd_timer_devices[ idx1 ] && idx1 < SND_CARDS - 1 ) {
          snd_oss_info_unregister( SND_OSS_INFO_DEV_TIMERS, idx1 );
          snd_oss_info_register( SND_OSS_INFO_DEV_TIMERS, idx1, snd_timer_devices[ idx1 ] -> name );
        }
      }
      snd_oss_info_unregister( SND_OSS_INFO_DEV_TIMERS, idx );
      snd_timer_devices[ idx ] = timer;
      break;
    }
  }
  if ( idx < SND_CARDS - 1 )
    snd_oss_info_register( SND_OSS_INFO_DEV_TIMERS, idx, timer -> name );  
  snd_mutex_up_static( register );
  return 0;
}

int snd_timer_unregister( snd_timer_t *timer )
{
  int idx;

  if ( !timer ) return -EINVAL;
  snd_mutex_down_static( register );
  for ( idx = 0; idx < SND_TIMER_DEVICES; idx++ ) {
    if ( snd_timer_devices[ idx ] == timer ) {
      snd_timer_devices[ idx ] = NULL;
      if ( idx < SND_CARDS - 1 )
        snd_oss_info_unregister( SND_OSS_INFO_DEV_TIMERS, idx );  
      snd_mutex_up_static( register );
      return snd_timer_free( timer );
    }
  }
  snd_mutex_up_static( register );
  return -EINVAL;
}

/*
 *  System timer
 */

unsigned int snd_timer_system_resolution( void )
{
  return 1000000000L / HZ;
}

static void snd_timer_s_function( unsigned long data )
{
  snd_timer_t *timer;
  
  timer = (snd_timer_t *)data;
  timer -> flags &= ~SND_TIMER_FLG_RUNNING;
  if ( timer -> callback ) 
    timer -> callback( timer, timer -> callback_data );
}

static void snd_timer_s_start( snd_timer_t *timer )
{
  struct timer_list *tlist;
  
  tlist = (struct timer_list *)timer -> private_data;
  tlist -> expires = jiffies + timer -> cticks;
  timer -> flags |= SND_TIMER_FLG_RUNNING;
  add_timer( tlist );
}

static void snd_timer_s_stop( snd_timer_t *timer )
{
  struct timer_list *tlist;
  
  tlist = (struct timer_list *)timer -> private_data;
  del_timer( tlist );  
  timer -> flags &= ~SND_TIMER_FLG_RUNNING;
  timer -> cticks = tlist -> expires - jiffies;
}

static void snd_timer_s_continue( snd_timer_t *timer )
{
  struct timer_list *tlist;
  
  tlist = (struct timer_list *)timer -> private_data;
  timer -> flags |= SND_TIMER_FLG_RUNNING;
  tlist -> expires = jiffies + timer -> cticks;
  add_timer( tlist );
}

static struct snd_stru_timer_hardware snd_timer_system = {
  1000000000L / HZ,		/* resolution in us */
  1,				/* low ticks */
  10000000L,			/* high ticks */
  NULL,				/* open */
  NULL,				/* close */
  NULL,				/* resolution */
  snd_timer_s_start,		/* start */
  snd_timer_s_stop,		/* stop */
  snd_timer_s_continue,		/* continue */
};

snd_timer_t *snd_timer_open_system( char *owner )
{
  snd_timer_t *timer;
  struct timer_list *tlist;
  
  timer = snd_timer_new_device( NULL, "system" );
  if ( !timer ) return NULL;
  strcpy( timer -> name, "system timer" );
  memcpy( &timer -> hw, &snd_timer_system, sizeof( snd_timer_system ) );
  timer -> flags |= SND_TIMER_FLG_USED | SND_TIMER_FLG_SYSTEM;
  tlist = (struct timer_list *)snd_calloc( sizeof( struct timer_list ) );
  if ( !tlist ) {
    snd_timer_free( timer );
    return NULL;
  }
  tlist -> function = snd_timer_s_function;
  tlist -> data = (unsigned long)timer;
  timer -> private_data = tlist;
  MOD_INC_USE_COUNT;
  return timer;
}

int snd_timer_close_system( snd_timer_t *timer )
{
  snd_free( timer -> private_data, sizeof( struct timer_list ) );
  snd_timer_free( timer );
  MOD_DEC_USE_COUNT;
  return 0;
}

/*
 *  Info interface
 */

static void snd_timer_proc_read( snd_info_buffer_t *buffer, void *private_data )
{
  int idx;
  unsigned int tmp;
  snd_timer_t *timer;

  snd_mutex_down_static( register );
  snd_mutex_down_static( open );
  for ( idx = 0; idx < SND_TIMER_DEVICES; idx++ ) {
    timer = snd_timer_devices[ idx ];
    if ( timer == NULL ) continue;
    snd_iprintf( buffer, "%02i-", idx );
    if ( timer -> card )
      snd_iprintf( buffer, "%02i: ", timer -> card -> number );
     else
      snd_iprintf( buffer, "XX: " );
    snd_iprintf( buffer, timer -> name );
    if ( timer -> hw.resolution )
      snd_iprintf( buffer, " : %u.%uus (%u-%u)", timer -> hw.resolution / 1000, timer -> hw.resolution % 1000, timer -> hw.low_ticks, timer -> hw.high_ticks );
    if ( timer -> flags & SND_TIMER_FLG_USED )
      snd_iprintf( buffer, " : %s", timer -> owner ? timer -> owner : "uknown" );
    snd_iprintf( buffer, "\n" );
  }
  tmp = snd_timer_system_resolution();
  snd_iprintf( buffer, "XX-XX: unlimited system timer : %i.%ius (%u-%u)\n", tmp / 1000, tmp % 1000, snd_timer_system.low_ticks, snd_timer_system.high_ticks );
  snd_mutex_up_static( open );
  snd_mutex_up_static( register );
}

/*
 *  ENTRY functions
 */

#ifndef LINUX_2_1
extern struct symbol_table snd_symbol_table_timer_export;
#endif

static snd_info_entry_t *snd_timer_proc_entry = NULL;

int init_module( void )
{
  snd_info_entry_t *entry;

#ifndef LINUX_2_1
  if ( register_symtab( &snd_symbol_table_timer_export ) < 0 )
    return -ENOMEM;
#endif
  memset( snd_timer_devices, 0, sizeof( snd_timer_devices ) );
  snd_oss_info_register( SND_OSS_INFO_DEV_TIMERS, SND_CARDS - 1, "system timer" );  
  if ( (entry = snd_info_create_entry( NULL, "timers" )) != NULL ) {
    entry -> t.text.read_size = SND_TIMER_DEVICES * 128;
    entry -> t.text.read = snd_timer_proc_read;
    if ( snd_info_register( entry ) < 0 ) {
      snd_info_free_entry( entry );
      entry = NULL;
    }
  }
  snd_timer_proc_entry = entry;
  return 0;
}

void cleanup_module( void )
{
  if ( snd_timer_proc_entry ) {
    snd_info_unregister( snd_timer_proc_entry );
    snd_timer_proc_entry = NULL;
  }
  snd_oss_info_unregister( SND_OSS_INFO_DEV_TIMERS, SND_CARDS - 1 );
}