summaryrefslogtreecommitdiff
path: root/sound/oss/emu10k1/timer.c
blob: d10d30739f41f49206b8e69b71dfafab8abd91bb (plain)
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

/*
 **********************************************************************
 *     timer.c
 *     Copyright (C) 1999, 2000 Creative Labs, inc.
 *
 **********************************************************************
 *
 *     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.
 *
 **********************************************************************
 */

/* 3/6/2000	Improved support for different timer delays  Rui Sousa */

/* 4/3/2000	Implemented timer list using list.h 	     Rui Sousa */

#include "hwaccess.h"
#include "8010.h"
#include "irqmgr.h"
#include "timer.h"

/* Try to schedule only once per fragment */

void emu10k1_timer_irqhandler(struct emu10k1_card *card)
{
	struct emu_timer *t;
	struct list_head *entry;

	spin_lock(&card->timer_lock);

	list_for_each(entry, &card->timers) {
		t = list_entry(entry, struct emu_timer, list);

		if (t->state & TIMER_STATE_ACTIVE) {
			t->count++;
			if (t->count == t->count_max) {
				t->count = 0;
				tasklet_hi_schedule(&t->tasklet);
			}
		}
	}

	spin_unlock(&card->timer_lock);

	return;
}

void emu10k1_timer_install(struct emu10k1_card *card, struct emu_timer *timer, u16 delay)
{
	struct emu_timer *t;
	struct list_head *entry;
	unsigned long flags;

	if (delay < 5)
		delay = 5;

	timer->delay = delay;
	timer->state = TIMER_STATE_INSTALLED;

	spin_lock_irqsave(&card->timer_lock, flags);

	timer->count_max = timer->delay / (card->timer_delay < 1024 ? card->timer_delay : 1024);
	timer->count = timer->count_max - 1;

	list_add(&timer->list, &card->timers);

	if (card->timer_delay > delay) {
		if (card->timer_delay == TIMER_STOPPED)
			emu10k1_irq_enable(card, INTE_INTERVALTIMERENB);

		card->timer_delay = delay;
		delay = (delay < 1024 ? delay : 1024);

		emu10k1_timer_set(card, delay);

		list_for_each(entry, &card->timers) {
			t = list_entry(entry, struct emu_timer, list);

			t->count_max = t->delay / delay;
			/* don't want to think much, just force scheduling 
			   on the next interrupt */
			t->count = t->count_max - 1;
		}

		DPD(2, "timer rate --> %u\n", delay);
	}

	spin_unlock_irqrestore(&card->timer_lock, flags);

	return;
}

void emu10k1_timer_uninstall(struct emu10k1_card *card, struct emu_timer *timer)
{
	struct emu_timer *t;
	struct list_head *entry;
	u16 delay = TIMER_STOPPED;
	unsigned long flags;

	if (timer->state == TIMER_STATE_UNINSTALLED)
		return;

	spin_lock_irqsave(&card->timer_lock, flags);

	list_del(&timer->list);

	list_for_each(entry, &card->timers) {
		t = list_entry(entry, struct emu_timer, list);

		if (t->delay < delay)
			delay = t->delay;
	}

	if (card->timer_delay != delay) {
		card->timer_delay = delay;

		if (delay == TIMER_STOPPED)
			emu10k1_irq_disable(card, INTE_INTERVALTIMERENB);
		else {
			delay = (delay < 1024 ? delay : 1024);

			emu10k1_timer_set(card, delay);

			list_for_each(entry, &card->timers) {
				t = list_entry(entry, struct emu_timer, list);

				t->count_max = t->delay / delay;
				t->count = t->count_max - 1;
			}
		}

		DPD(2, "timer rate --> %u\n", delay);
	}

	spin_unlock_irqrestore(&card->timer_lock, flags);

	timer->state = TIMER_STATE_UNINSTALLED;

	return;
}

void emu10k1_timer_enable(struct emu10k1_card *card, struct emu_timer *timer)
{
	unsigned long flags;

	spin_lock_irqsave(&card->timer_lock, flags);
	timer->state |= TIMER_STATE_ACTIVE;
	spin_unlock_irqrestore(&card->timer_lock, flags);

	return;
}

void emu10k1_timer_disable(struct emu10k1_card *card, struct emu_timer *timer)
{
	unsigned long flags;

	spin_lock_irqsave(&card->timer_lock, flags);
	timer->state &= ~TIMER_STATE_ACTIVE;
	spin_unlock_irqrestore(&card->timer_lock, flags);

	return;
}