Home | History | Annotate | Line # | Download | only in isa
spkr_pcppi.c revision 1.1
      1 /*	$NetBSD: spkr_pcppi.c,v 1.1 2016/12/09 02:22:34 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1990 Eric S. Raymond (esr (at) snark.thyrsus.com)
      5  * Copyright (c) 1990 Andrew A. Chernov (ache (at) astral.msk.su)
      6  * Copyright (c) 1990 Lennart Augustsson (lennart (at) 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 (at) snark.thyrsus.com) Feb 1990
     40  *      modified for 386bsd by Andrew A. Chernov <ache (at) 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.1 2016/12/09 02:22:34 christos 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/module.h>
     55 #include <sys/uio.h>
     56 #include <sys/proc.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/conf.h>
     59 
     60 #include <sys/bus.h>
     61 
     62 #include <dev/isa/pcppivar.h>
     63 
     64 #include <dev/isa/spkrio.h>
     65 
     66 void spkrattach(device_t, device_t, void *);
     67 int spkrdetach(device_t, int);
     68 int spkrprobe(device_t, cfdata_t, void *);
     69 
     70 #include "ioconf.h"
     71 
     72 MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "pcppi" */);
     73 
     74 #ifdef _MODULE
     75 #include "ioconf.c"
     76 #endif
     77 
     78 CFATTACH_DECL_NEW(spkr_pcppi, 0, spkrprobe, spkrattach, spkrdetach, NULL);
     79 
     80 static pcppi_tag_t ppicookie;
     81 
     82 #define SPKRPRI (PZERO - 1)
     83 
     84 void
     85 spkr_tone(u_int xhz, u_int ticks)
     86 /* emit tone of frequency hz for given number of ticks */
     87 {
     88 	pcppi_bell(ppicookie, xhz, ticks, PCPPI_BELL_SLEEP);
     89 }
     90 
     91 void
     92 spkr_rest(int ticks)
     93 /* rest for given number of ticks */
     94 {
     95     /*
     96      * Set timeout to endrest function, then give up the timeslice.
     97      * This is so other processes can execute while the rest is being
     98      * waited out.
     99      */
    100 #ifdef SPKRDEBUG
    101     printf("%s: %d\n", __func__, ticks);
    102 #endif /* SPKRDEBUG */
    103     if (ticks > 0)
    104 	    tsleep(spkr_rest, SPKRPRI | PCATCH, "rest", ticks);
    105 }
    106 
    107 extern int spkr_active;	/* exclusion flag */
    108 extern const struct cdevsw spkr_cdevsw;
    109 
    110 int spkr_attached = 0;
    111 
    112 int
    113 spkrprobe(device_t parent, cfdata_t match, void *aux)
    114 {
    115 	return (!spkr_attached);
    116 }
    117 
    118 void
    119 spkrattach(device_t parent, device_t self, void *aux)
    120 {
    121 	aprint_naive("\n");
    122 	aprint_normal("\n");
    123 	ppicookie = ((struct pcppi_attach_args *)aux)->pa_cookie;
    124 	spkr_attached = 1;
    125 	if (!pmf_device_register(self, NULL, NULL))
    126 		aprint_error_dev(self, "couldn't establish power handler\n");
    127 }
    128 
    129 int
    130 spkrdetach(device_t self, int flags)
    131 {
    132 
    133 	pmf_device_deregister(self);
    134 	spkr_attached = 0;
    135 	ppicookie = NULL;
    136 
    137 	return 0;
    138 }
    139 
    140 
    141 static int
    142 spkr_modcmd(modcmd_t cmd, void *arg)
    143 {
    144 #ifdef _MODULE
    145 	devmajor_t bmajor, cmajor;
    146 #endif
    147 	int error = 0;
    148 
    149 #ifdef _MODULE
    150 	switch(cmd) {
    151 	case MODULE_CMD_INIT:
    152 		bmajor = cmajor = -1;
    153 		error = devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
    154 		    &spkr_cdevsw, &cmajor);
    155 		if (error)
    156 			break;
    157 
    158 		error = config_init_component(cfdriver_ioconf_spkr,
    159 			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
    160 		if (error) {
    161 			devsw_detach(NULL, &spkr_cdevsw);
    162 		}
    163 		break;
    164 
    165 	case MODULE_CMD_FINI:
    166 		if (spkr_active)
    167 			return EBUSY;
    168 		error = config_fini_component(cfdriver_ioconf_spkr,
    169 			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
    170 		devsw_detach(NULL, &spkr_cdevsw);
    171 		break;
    172 	default:
    173 		error = ENOTTY;
    174 		break;
    175 	}
    176 #endif
    177 
    178 	return error;
    179 }
    180