summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/io/beeper.c
blob: fff0a7a96226021eda8def77198f670f23c564b8 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Simple beeper support for PC platform, using standard timer 2 beeper.
 * Eventually this should probably be in a driver.
 */

#include <sys/types.h>
#include <sys/conf.h>
#include <sys/beep.h>
#include <sys/ksynch.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/errno.h>
#include <sys/inttypes.h>

#define	NELEM(a)	(sizeof (a) / sizeof ((a)[0]))

#define	TIMER		0x40
#define	TIMERCR		(TIMER+3)
#define	TIMER2		(TIMER+2)

#define	TONE_CTL	0x61
#define	T_CTLWORD	0xB6
#define	TONE_ON		0x03

static struct beep_state {
	enum {BEEP_OFF, BEEP_TIMED, BEEP_ON} state;
	kmutex_t mutex;
} beep_state;

static void beep_end(void *);
static void beep_on(void);
static void beep_off(void);
static void beep_frequency(int frequency);

struct beep_params {
	enum beep_type	type;
	int		frequency;	/* Hz */
	int		duration;	/* milliseconds */
};

struct beep_params beep_params[] = {
	{ BEEP_CONSOLE,	900,	200 },
	{ BEEP_TYPE4,	2000,	0 },
	{ BEEP_DEFAULT,	1000,	200 }	/* Must be last */
};

/* ARGSUSED */
void
beep(enum beep_type type)
{
	struct beep_params *bp;

	/*
	 * In the fullness of time, we would use the "type" argument
	 * to determine frequency, volume, duration, waveform, sample
	 * to be played, ...
	 */

	for (bp = beep_params; bp->type != BEEP_DEFAULT; bp++) {
		if (bp->type == type)
			break;
	}

	mutex_enter(&beep_state.mutex);
	if (beep_state.state == BEEP_OFF && bp->frequency != 0) {
	    beep_state.state = BEEP_TIMED;
	    beep_frequency(bp->frequency);
	    beep_on();
	    (void) timeout(beep_end, &beep_state,
			drv_usectohz(bp->duration*1000));
	}
	mutex_exit(&beep_state.mutex);
}

/* ARGSUSED */
void
beep_polled(enum beep_type type)
{
	/* No-op at this time */
}

static void
beep_end(void *arg)
{
	struct beep_state *state = arg;

	/* Perhaps we should enforce a small "quiet" period between beeps. */
	mutex_enter(&state->mutex);
	state->state = BEEP_OFF;
	beep_off();
	mutex_exit(&state->mutex);
}

static void
beep_frequency(int frequency)
{
	int counter;

	counter = 1193180 / frequency;
	if (counter > 65535)
		counter = 65535;
	else if (counter < 1)
		counter = 1;

	outb(TIMERCR, T_CTLWORD);
	outb(TIMER2, counter & 0xff);
	outb(TIMER2, counter >> 8);
}

static void
beep_on(void)
{
	outb(TONE_CTL, inb(TONE_CTL) | TONE_ON);
}

static void
beep_off(void)
{
	outb(TONE_CTL, inb(TONE_CTL) & ~TONE_ON);
}

void
beeper_on(enum beep_type type)
{
	struct beep_params *bp;

	for (bp = beep_params; bp->type != BEEP_DEFAULT; bp++) {
		if (bp->type == type)
			break;
	}

	mutex_enter(&beep_state.mutex);
	if (beep_state.state == BEEP_OFF) {
		beep_state.state = BEEP_ON;
		if (bp->frequency != 0) {
			beep_frequency(bp->frequency);
			beep_on();
		}
	}
	mutex_exit(&beep_state.mutex);
}

void
beeper_off()
{
	mutex_enter(&beep_state.mutex);
	if (beep_state.state == BEEP_ON) {
		beep_state.state = BEEP_OFF;
		beep_off();
	}
	mutex_exit(&beep_state.mutex);
}

int
beeper_freq(enum beep_type type, int freq)
{
	struct beep_params *bp;

	/*
	 * The frequency value is limited to the range of [0 - 32767]
	 */
	if ((type != BEEP_CONSOLE && type != BEEP_TYPE4) || freq < 0 ||
	    freq > INT16_MAX)
		return (EINVAL);

	for (bp = beep_params; bp->type != BEEP_DEFAULT; bp++) {
		if (bp->type == type)
			break;
	}

	bp->frequency = freq;
	return (0);
}