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