xref: /netbsd-src/sys/dev/isa/spkr_pcppi.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: spkr_pcppi.c,v 1.11 2017/06/14 05:01:35 pgoyette Exp $	*/
2 
3 /*
4  * Copyright (c) 1990 Eric S. Raymond (esr@snark.thyrsus.com)
5  * Copyright (c) 1990 Andrew A. Chernov (ache@astral.msk.su)
6  * Copyright (c) 1990 Lennart Augustsson (lennart@augustsson.net)
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by Eric S. Raymond
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * spkr.c -- device driver for console speaker on 80386
38  *
39  * v1.1 by Eric S. Raymond (esr@snark.thyrsus.com) Feb 1990
40  *      modified for 386bsd by Andrew A. Chernov <ache@astral.msk.su>
41  *      386bsd only clean version, all SYSV stuff removed
42  *      use hz value from param.c
43  */
44 
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: spkr_pcppi.c,v 1.11 2017/06/14 05:01:35 pgoyette Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53 #include <sys/malloc.h>
54 #include <sys/uio.h>
55 #include <sys/proc.h>
56 #include <sys/ioctl.h>
57 #include <sys/conf.h>
58 
59 #include <sys/bus.h>
60 
61 #include <dev/isa/pcppivar.h>
62 
63 #include <dev/spkrvar.h>
64 #include <dev/spkrio.h>
65 
66 struct spkr_pcppi_softc {
67 	struct spkr_softc sc_spkr;
68 	pcppi_tag_t sc_pcppicookie;
69 	void (*sc_bell_func)(pcppi_tag_t, int, int, int);
70 };
71 
72 static int spkr_pcppi_probe(device_t, cfdata_t, void *);
73 static void spkr_pcppi_attach(device_t, device_t, void *);
74 static int spkr_pcppi_detach(device_t, int);
75 static int spkr_pcppi_rescan(device_t, const char *, const int *);
76 static void spkr_pcppi_childdet(device_t, device_t);
77 
78 CFATTACH_DECL3_NEW(spkr_pcppi, sizeof(struct spkr_pcppi_softc),
79     spkr_pcppi_probe, spkr_pcppi_attach, spkr_pcppi_detach, NULL,
80     spkr_pcppi_rescan, spkr_pcppi_childdet, 0);
81 
82 #define SPKRPRI (PZERO - 1)
83 
84 /* emit tone of frequency hz for given number of ticks */
85 static void
86 spkr_pcppi_tone(device_t self, u_int xhz, u_int ticks)
87 {
88 #ifdef SPKRDEBUG
89 	aprint_debug_dev(self, "%s: %u %u\n", __func__, xhz, ticks);
90 #endif /* SPKRDEBUG */
91 	struct spkr_pcppi_softc *sc = device_private(self);
92 	(*sc->sc_bell_func)(sc->sc_pcppicookie, xhz, ticks, PCPPI_BELL_SLEEP);
93 }
94 
95 /* rest for given number of ticks */
96 static void
97 spkr_pcppi_rest(device_t self, int ticks)
98 {
99 	/*
100 	 * Set timeout to endrest function, then give up the timeslice.
101 	 * This is so other processes can execute while the rest is being
102 	 * waited out.
103 	 */
104 #ifdef SPKRDEBUG
105 	aprint_debug_dev(self, "%s: %d\n", __func__, ticks);
106 #endif /* SPKRDEBUG */
107 	if (ticks > 0)
108 		tsleep(self, SPKRPRI | PCATCH, device_xname(self), ticks);
109 }
110 
111 static int
112 spkr_pcppi_probe(device_t parent, cfdata_t cf, void *aux)
113 {
114 	return 1;
115 }
116 
117 static void
118 spkr_pcppi_attach(device_t parent, device_t self, void *aux)
119 {
120 	struct pcppi_attach_args *pa = aux;
121 	struct spkr_pcppi_softc *sc = device_private(self);
122 
123 	aprint_naive("\n");
124 	aprint_normal(": PC Speaker\n");
125 
126 	sc->sc_pcppicookie = pa->pa_cookie;
127 	sc->sc_bell_func = pa->pa_bell_func;
128 	spkr_attach(self, spkr_pcppi_tone, spkr_pcppi_rest);
129 	if (!pmf_device_register(self, NULL, NULL))
130 		aprint_error_dev(self, "couldn't establish power handler\n");
131 }
132 
133 static int
134 spkr_pcppi_detach(device_t self, int flags)
135 {
136 	struct spkr_pcppi_softc *sc = device_private(self);
137 	int error;
138 
139 	if ((error = spkr_detach(self, flags)) != 0)
140 		return error;
141 
142 	sc->sc_pcppicookie = NULL;
143 	pmf_device_deregister(self);
144 	return 0;
145 }
146 
147 static int
148 spkr_pcppi_rescan(device_t self, const char *iattr, const int *locators)
149 {
150 
151 	return spkr_rescan(self, iattr, locators);
152 }
153 
154 static void
155 spkr_pcppi_childdet(device_t self, device_t child)
156 {
157 
158 	spkr_childdet(self, child);
159 }
160