Home | History | Annotate | Line # | Download | only in dev
radio.c revision 1.21
      1 /* $NetBSD: radio.c,v 1.21 2008/06/08 18:18:33 tsutsui 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.21 2008/06/08 18:18:33 tsutsui 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 CFATTACH_DECL(radio, sizeof(struct radio_softc),
     55     radioprobe, radioattach, radiodetach, radioactivate);
     56 
     57 dev_type_open(radioopen);
     58 dev_type_close(radioclose);
     59 dev_type_ioctl(radioioctl);
     60 
     61 const struct cdevsw radio_cdevsw = {
     62 	radioopen, radioclose, noread, nowrite, radioioctl,
     63 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
     64 };
     65 
     66 extern struct cfdriver radio_cd;
     67 
     68 int
     69 radioprobe(struct device *parent, struct cfdata *match,
     70     void *aux)
     71 {
     72 	return (1);
     73 }
     74 
     75 void
     76 radioattach(struct device *parent, struct device *self, void *aux)
     77 {
     78 	struct radio_softc *sc = (void *)self;
     79 	struct radio_attach_args *sa = aux;
     80 	const struct radio_hw_if *hwp = sa->hwif;
     81 	void  *hdlp = sa->hdl;
     82 
     83 	printf("\n");
     84 	sc->hw_if = hwp;
     85 	sc->hw_hdl = hdlp;
     86 	sc->sc_dev = parent;
     87 }
     88 
     89 int
     90 radioopen(dev_t dev, int flags, int fmt, struct lwp *l)
     91 {
     92 	int	unit;
     93 	struct radio_softc *sc;
     94 
     95 	unit = RADIOUNIT(dev);
     96 	sc = device_lookup_private(&radio_cd, unit);
     97 	if (sc == NULL || sc->hw_if == NULL)
     98 		return (ENXIO);
     99 
    100 	if (sc->hw_if->open != NULL)
    101 		return (sc->hw_if->open(sc->hw_hdl, flags, fmt, l->l_proc));
    102 	else
    103 		return (0);
    104 }
    105 
    106 int
    107 radioclose(dev_t dev, int flags, int fmt, struct lwp *l)
    108 {
    109 	struct radio_softc *sc;
    110 
    111 	sc = device_lookup_private(&radio_cd, RADIOUNIT(dev));
    112 
    113 	if (sc->hw_if->close != NULL)
    114 		return (sc->hw_if->close(sc->hw_hdl, flags, fmt, l->l_proc));
    115 	else
    116 		return (0);
    117 }
    118 
    119 int
    120 radioioctl(dev_t dev, u_long cmd, void *data, int flags,
    121     struct lwp *l)
    122 {
    123 	struct radio_softc *sc;
    124 	int unit, error;
    125 
    126 	unit = RADIOUNIT(dev);
    127 	sc = device_lookup_private(&radio_cd, unit);
    128 	if (sc == NULL || sc->hw_if == NULL)
    129 		return (ENXIO);
    130 
    131 	error = EOPNOTSUPP;
    132 	switch (cmd) {
    133 	case RIOCGINFO:
    134 		if (sc->hw_if->get_info)
    135 			error = (sc->hw_if->get_info)(sc->hw_hdl,
    136 					(struct radio_info *)data);
    137 			break;
    138 	case RIOCSINFO:
    139 		if (sc->hw_if->set_info)
    140 			error = (sc->hw_if->set_info)(sc->hw_hdl,
    141 				(struct radio_info *)data);
    142 		break;
    143 	case RIOCSSRCH:
    144 		if (sc->hw_if->search)
    145 			error = (sc->hw_if->search)(sc->hw_hdl,
    146 					*(int *)data);
    147 		break;
    148 	default:
    149 		error = EINVAL;
    150 	}
    151 
    152 	return (error);
    153 }
    154 
    155 /*
    156  * Called from hardware driver. This is where the MI radio driver gets
    157  * probed/attached to the hardware driver
    158  */
    159 struct device *
    160 radio_attach_mi(const struct radio_hw_if *rhwp, void *hdlp, struct device *dev)
    161 {
    162 	struct radio_attach_args arg;
    163 
    164 	arg.hwif = rhwp;
    165 	arg.hdl = hdlp;
    166 	return (config_found(dev, &arg, radioprint));
    167 }
    168 
    169 int
    170 radioprint(void *aux, const char *pnp)
    171 {
    172 	if (pnp != NULL)
    173 		aprint_normal("radio at %s", pnp);
    174 	return (UNCONF);
    175 }
    176 
    177 int
    178 radiodetach(struct device *self, int flags)
    179 {
    180 	/*struct radio_softc *sc = (struct radio_softc *)self;*/
    181 	int maj, mn;
    182 
    183 	/* locate the major number */
    184 	maj = cdevsw_lookup_major(&radio_cdevsw);
    185 
    186 	/* Nuke the vnodes for any open instances (calls close). */
    187 	mn = device_unit(self);
    188 	vdevgone(maj, mn, mn, VCHR);
    189 
    190 	return (0);
    191 }
    192 
    193 int
    194 radioactivate(struct device *self, enum devact act)
    195 {
    196 	struct radio_softc *sc = (struct radio_softc *)self;
    197 
    198 	switch (act) {
    199 	case DVACT_ACTIVATE:
    200 		return (EOPNOTSUPP);
    201 
    202 	case DVACT_DEACTIVATE:
    203 		sc->sc_dying = 1;
    204 		break;
    205 	}
    206 	return (0);
    207 }
    208