Home | History | Annotate | Line # | Download | only in dev
radio.c revision 1.4
      1 /* $NetBSD: radio.c,v 1.4 2002/09/06 13:18:43 gehenna Exp $ */
      2 /* $OpenBSD: radio.c,v 1.2 2001/12/05 10:27:06 mickey Exp $ */
      3 /* $RuOBSD: radio.c,v 1.7 2001/12/04 06:03:05 tm Exp $ */
      4 
      5 /*
      6  * Copyright (c) 2001 Maxim Tsyplakov <tm (at) oganer.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  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /* This is the /dev/radio driver from OpenBSD */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: radio.c,v 1.4 2002/09/06 13:18:43 gehenna Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/proc.h>
     38 #include <sys/errno.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/device.h>
     41 #include <sys/vnode.h>
     42 #include <sys/radioio.h>
     43 #include <sys/conf.h>
     44 
     45 #include <dev/radio_if.h>
     46 #include <dev/radiovar.h>
     47 
     48 int	radioprobe(struct device *, struct cfdata *, void *);
     49 void	radioattach(struct device *, struct device *, void *);
     50 int	radioprint(void *, const char *);
     51 int	radiodetach(struct device *, int);
     52 int	radioactivate(struct device *, enum devact);
     53 
     54 struct cfattach radio_ca = {
     55 	sizeof(struct radio_softc), radioprobe, radioattach,
     56 	radiodetach, radioactivate
     57 };
     58 
     59 dev_type_open(radioopen);
     60 dev_type_close(radioclose);
     61 dev_type_ioctl(radioioctl);
     62 
     63 const struct cdevsw radio_cdevsw = {
     64 	radioopen, radioclose, noread, nowrite, radioioctl,
     65 	nostop, notty, nopoll, nommap,
     66 };
     67 
     68 extern struct cfdriver radio_cd;
     69 
     70 int
     71 radioprobe(struct device *parent, struct cfdata *match, void *aux)
     72 {
     73 	return (1);
     74 }
     75 
     76 void
     77 radioattach(struct device *parent, struct device *self, void *aux)
     78 {
     79 	struct radio_softc *sc = (void *)self;
     80 	struct radio_attach_args *sa = aux;
     81 	struct radio_hw_if *hwp = sa->hwif;
     82 	void  *hdlp = sa->hdl;
     83 
     84 	printf("\n");
     85 	sc->hw_if = hwp;
     86 	sc->hw_hdl = hdlp;
     87 	sc->sc_dev = parent;
     88 }
     89 
     90 int
     91 radioopen(dev_t dev, int flags, int fmt, struct proc *p)
     92 {
     93 	int	unit;
     94 	struct radio_softc *sc;
     95 
     96 	unit = RADIOUNIT(dev);
     97 	if (unit >= radio_cd.cd_ndevs ||
     98 	    (sc = radio_cd.cd_devs[unit]) == NULL ||
     99 	     sc->hw_if == NULL)
    100 		return (ENXIO);
    101 
    102 	if (sc->hw_if->open != NULL)
    103 		return (sc->hw_if->open(sc->hw_hdl, flags, fmt, p));
    104 	else
    105 		return (0);
    106 }
    107 
    108 int
    109 radioclose(dev_t dev, int flags, int fmt, struct proc *p)
    110 {
    111 	struct radio_softc *sc;
    112 
    113 	sc = radio_cd.cd_devs[RADIOUNIT(dev)];
    114 
    115 	if (sc->hw_if->close != NULL)
    116 		return (sc->hw_if->close(sc->hw_hdl, flags, fmt, p));
    117 	else
    118 		return (0);
    119 }
    120 
    121 int
    122 radioioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
    123 {
    124 	struct radio_softc *sc;
    125 	int unit, error;
    126 
    127 	unit = RADIOUNIT(dev);
    128 	if (unit >= radio_cd.cd_ndevs ||
    129 	    (sc = radio_cd.cd_devs[unit]) == NULL || sc->hw_if == NULL)
    130 		return (ENXIO);
    131 
    132 	error = EOPNOTSUPP;
    133 	switch (cmd) {
    134 	case RIOCGINFO:
    135 		if (sc->hw_if->get_info)
    136 			error = (sc->hw_if->get_info)(sc->hw_hdl,
    137 					(struct radio_info *)data);
    138 			break;
    139 	case RIOCSINFO:
    140 		if (sc->hw_if->set_info)
    141 			error = (sc->hw_if->set_info)(sc->hw_hdl,
    142 				(struct radio_info *)data);
    143 		break;
    144 	case RIOCSSRCH:
    145 		if (sc->hw_if->search)
    146 			error = (sc->hw_if->search)(sc->hw_hdl,
    147 					*(int *)data);
    148 		break;
    149 	default:
    150 		error = EINVAL;
    151 	}
    152 
    153 	return (error);
    154 }
    155 
    156 /*
    157  * Called from hardware driver. This is where the MI radio driver gets
    158  * probed/attached to the hardware driver
    159  */
    160 struct device *
    161 radio_attach_mi(struct radio_hw_if *rhwp, void *hdlp, struct device *dev)
    162 {
    163 	struct radio_attach_args arg;
    164 
    165 	arg.hwif = rhwp;
    166 	arg.hdl = hdlp;
    167 	return (config_found(dev, &arg, radioprint));
    168 }
    169 
    170 int
    171 radioprint(void *aux, const char *pnp)
    172 {
    173 	if (pnp != NULL)
    174 		printf("radio at %s", pnp);
    175 	return (UNCONF);
    176 }
    177 
    178 int
    179 radiodetach(struct device *self, int flags)
    180 {
    181 	/*struct radio_softc *sc = (struct radio_softc *)self;*/
    182 	int maj, mn;
    183 
    184 	/* locate the major number */
    185 	maj = cdevsw_lookup_major(&radio_cdevsw);
    186 
    187 	/* Nuke the vnodes for any open instances (calls close). */
    188 	mn = self->dv_unit;
    189 	vdevgone(maj, mn, mn, VCHR);
    190 
    191 	return (0);
    192 }
    193 
    194 int
    195 radioactivate(struct device *self, enum devact act)
    196 {
    197 	struct radio_softc *sc = (struct radio_softc *)self;
    198 
    199 	switch (act) {
    200 	case DVACT_ACTIVATE:
    201 		return (EOPNOTSUPP);
    202 		break;
    203 
    204 	case DVACT_DEACTIVATE:
    205 		sc->sc_dying = 1;
    206 		break;
    207 	}
    208 	return (0);
    209 }
    210