Home | History | Annotate | Line # | Download | only in isa
spkr_pcppi.c revision 1.1
      1  1.1  christos /*	$NetBSD: spkr_pcppi.c,v 1.1 2016/12/09 02:22:34 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1990 Eric S. Raymond (esr (at) snark.thyrsus.com)
      5  1.1  christos  * Copyright (c) 1990 Andrew A. Chernov (ache (at) astral.msk.su)
      6  1.1  christos  * Copyright (c) 1990 Lennart Augustsson (lennart (at) augustsson.net)
      7  1.1  christos  * All rights reserved.
      8  1.1  christos  *
      9  1.1  christos  * Redistribution and use in source and binary forms, with or without
     10  1.1  christos  * modification, are permitted provided that the following conditions
     11  1.1  christos  * are met:
     12  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  christos  *    documentation and/or other materials provided with the distribution.
     17  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     18  1.1  christos  *    must display the following acknowledgement:
     19  1.1  christos  *	This product includes software developed by Eric S. Raymond
     20  1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     21  1.1  christos  *    derived from this software without specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     25  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     26  1.1  christos  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     27  1.1  christos  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     28  1.1  christos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     29  1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     31  1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     32  1.1  christos  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos /*
     37  1.1  christos  * spkr.c -- device driver for console speaker on 80386
     38  1.1  christos  *
     39  1.1  christos  * v1.1 by Eric S. Raymond (esr (at) snark.thyrsus.com) Feb 1990
     40  1.1  christos  *      modified for 386bsd by Andrew A. Chernov <ache (at) astral.msk.su>
     41  1.1  christos  *      386bsd only clean version, all SYSV stuff removed
     42  1.1  christos  *      use hz value from param.c
     43  1.1  christos  */
     44  1.1  christos 
     45  1.1  christos #include <sys/cdefs.h>
     46  1.1  christos __KERNEL_RCSID(0, "$NetBSD: spkr_pcppi.c,v 1.1 2016/12/09 02:22:34 christos Exp $");
     47  1.1  christos 
     48  1.1  christos #include <sys/param.h>
     49  1.1  christos #include <sys/systm.h>
     50  1.1  christos #include <sys/kernel.h>
     51  1.1  christos #include <sys/errno.h>
     52  1.1  christos #include <sys/device.h>
     53  1.1  christos #include <sys/malloc.h>
     54  1.1  christos #include <sys/module.h>
     55  1.1  christos #include <sys/uio.h>
     56  1.1  christos #include <sys/proc.h>
     57  1.1  christos #include <sys/ioctl.h>
     58  1.1  christos #include <sys/conf.h>
     59  1.1  christos 
     60  1.1  christos #include <sys/bus.h>
     61  1.1  christos 
     62  1.1  christos #include <dev/isa/pcppivar.h>
     63  1.1  christos 
     64  1.1  christos #include <dev/isa/spkrio.h>
     65  1.1  christos 
     66  1.1  christos void spkrattach(device_t, device_t, void *);
     67  1.1  christos int spkrdetach(device_t, int);
     68  1.1  christos int spkrprobe(device_t, cfdata_t, void *);
     69  1.1  christos 
     70  1.1  christos #include "ioconf.h"
     71  1.1  christos 
     72  1.1  christos MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "pcppi" */);
     73  1.1  christos 
     74  1.1  christos #ifdef _MODULE
     75  1.1  christos #include "ioconf.c"
     76  1.1  christos #endif
     77  1.1  christos 
     78  1.1  christos CFATTACH_DECL_NEW(spkr_pcppi, 0, spkrprobe, spkrattach, spkrdetach, NULL);
     79  1.1  christos 
     80  1.1  christos static pcppi_tag_t ppicookie;
     81  1.1  christos 
     82  1.1  christos #define SPKRPRI (PZERO - 1)
     83  1.1  christos 
     84  1.1  christos void
     85  1.1  christos spkr_tone(u_int xhz, u_int ticks)
     86  1.1  christos /* emit tone of frequency hz for given number of ticks */
     87  1.1  christos {
     88  1.1  christos 	pcppi_bell(ppicookie, xhz, ticks, PCPPI_BELL_SLEEP);
     89  1.1  christos }
     90  1.1  christos 
     91  1.1  christos void
     92  1.1  christos spkr_rest(int ticks)
     93  1.1  christos /* rest for given number of ticks */
     94  1.1  christos {
     95  1.1  christos     /*
     96  1.1  christos      * Set timeout to endrest function, then give up the timeslice.
     97  1.1  christos      * This is so other processes can execute while the rest is being
     98  1.1  christos      * waited out.
     99  1.1  christos      */
    100  1.1  christos #ifdef SPKRDEBUG
    101  1.1  christos     printf("%s: %d\n", __func__, ticks);
    102  1.1  christos #endif /* SPKRDEBUG */
    103  1.1  christos     if (ticks > 0)
    104  1.1  christos 	    tsleep(spkr_rest, SPKRPRI | PCATCH, "rest", ticks);
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos extern int spkr_active;	/* exclusion flag */
    108  1.1  christos extern const struct cdevsw spkr_cdevsw;
    109  1.1  christos 
    110  1.1  christos int spkr_attached = 0;
    111  1.1  christos 
    112  1.1  christos int
    113  1.1  christos spkrprobe(device_t parent, cfdata_t match, void *aux)
    114  1.1  christos {
    115  1.1  christos 	return (!spkr_attached);
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos void
    119  1.1  christos spkrattach(device_t parent, device_t self, void *aux)
    120  1.1  christos {
    121  1.1  christos 	aprint_naive("\n");
    122  1.1  christos 	aprint_normal("\n");
    123  1.1  christos 	ppicookie = ((struct pcppi_attach_args *)aux)->pa_cookie;
    124  1.1  christos 	spkr_attached = 1;
    125  1.1  christos 	if (!pmf_device_register(self, NULL, NULL))
    126  1.1  christos 		aprint_error_dev(self, "couldn't establish power handler\n");
    127  1.1  christos }
    128  1.1  christos 
    129  1.1  christos int
    130  1.1  christos spkrdetach(device_t self, int flags)
    131  1.1  christos {
    132  1.1  christos 
    133  1.1  christos 	pmf_device_deregister(self);
    134  1.1  christos 	spkr_attached = 0;
    135  1.1  christos 	ppicookie = NULL;
    136  1.1  christos 
    137  1.1  christos 	return 0;
    138  1.1  christos }
    139  1.1  christos 
    140  1.1  christos 
    141  1.1  christos static int
    142  1.1  christos spkr_modcmd(modcmd_t cmd, void *arg)
    143  1.1  christos {
    144  1.1  christos #ifdef _MODULE
    145  1.1  christos 	devmajor_t bmajor, cmajor;
    146  1.1  christos #endif
    147  1.1  christos 	int error = 0;
    148  1.1  christos 
    149  1.1  christos #ifdef _MODULE
    150  1.1  christos 	switch(cmd) {
    151  1.1  christos 	case MODULE_CMD_INIT:
    152  1.1  christos 		bmajor = cmajor = -1;
    153  1.1  christos 		error = devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
    154  1.1  christos 		    &spkr_cdevsw, &cmajor);
    155  1.1  christos 		if (error)
    156  1.1  christos 			break;
    157  1.1  christos 
    158  1.1  christos 		error = config_init_component(cfdriver_ioconf_spkr,
    159  1.1  christos 			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
    160  1.1  christos 		if (error) {
    161  1.1  christos 			devsw_detach(NULL, &spkr_cdevsw);
    162  1.1  christos 		}
    163  1.1  christos 		break;
    164  1.1  christos 
    165  1.1  christos 	case MODULE_CMD_FINI:
    166  1.1  christos 		if (spkr_active)
    167  1.1  christos 			return EBUSY;
    168  1.1  christos 		error = config_fini_component(cfdriver_ioconf_spkr,
    169  1.1  christos 			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
    170  1.1  christos 		devsw_detach(NULL, &spkr_cdevsw);
    171  1.1  christos 		break;
    172  1.1  christos 	default:
    173  1.1  christos 		error = ENOTTY;
    174  1.1  christos 		break;
    175  1.1  christos 	}
    176  1.1  christos #endif
    177  1.1  christos 
    178  1.1  christos 	return error;
    179  1.1  christos }
    180