Home | History | Annotate | Line # | Download | only in dev
radio.c revision 1.25.18.1
      1  1.25.18.1     rmind /* $NetBSD: radio.c,v 1.25.18.1 2014/05/18 17:45:35 rmind Exp $ */
      2        1.1  augustss /* $OpenBSD: radio.c,v 1.2 2001/12/05 10:27:06 mickey Exp $ */
      3        1.1  augustss /* $RuOBSD: radio.c,v 1.7 2001/12/04 06:03:05 tm Exp $ */
      4        1.1  augustss 
      5        1.1  augustss /*
      6        1.1  augustss  * Copyright (c) 2001 Maxim Tsyplakov <tm (at) oganer.net>
      7        1.1  augustss  * All rights reserved.
      8        1.1  augustss  *
      9        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     10        1.1  augustss  * modification, are permitted provided that the following conditions
     11        1.1  augustss  * are met:
     12        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     13        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     14        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     16        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     17        1.1  augustss  *
     18        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19        1.1  augustss  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20        1.1  augustss  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21        1.1  augustss  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22        1.1  augustss  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23        1.1  augustss  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24        1.1  augustss  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25        1.1  augustss  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26        1.1  augustss  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27        1.1  augustss  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28        1.1  augustss  */
     29        1.1  augustss 
     30        1.1  augustss /* This is the /dev/radio driver from OpenBSD */
     31        1.1  augustss 
     32        1.2  augustss #include <sys/cdefs.h>
     33  1.25.18.1     rmind __KERNEL_RCSID(0, "$NetBSD: radio.c,v 1.25.18.1 2014/05/18 17:45:35 rmind Exp $");
     34        1.2  augustss 
     35        1.1  augustss #include <sys/param.h>
     36        1.1  augustss #include <sys/systm.h>
     37        1.1  augustss #include <sys/proc.h>
     38        1.1  augustss #include <sys/errno.h>
     39        1.1  augustss #include <sys/ioctl.h>
     40        1.1  augustss #include <sys/device.h>
     41        1.2  augustss #include <sys/vnode.h>
     42        1.1  augustss #include <sys/radioio.h>
     43        1.2  augustss #include <sys/conf.h>
     44        1.1  augustss 
     45        1.1  augustss #include <dev/radio_if.h>
     46        1.1  augustss 
     47       1.22     joerg struct radio_softc {
     48       1.22     joerg 	void		*hw_hdl;	/* hardware driver handle */
     49       1.22     joerg 	device_t 	sc_dev;		/* hardware device struct */
     50       1.22     joerg 	const struct radio_hw_if *hw_if; /* hardware interface */
     51       1.22     joerg };
     52       1.22     joerg 
     53       1.22     joerg static int	radioprobe(device_t, cfdata_t, void *);
     54       1.22     joerg static void	radioattach(device_t, device_t, void *);
     55       1.22     joerg static int	radioprint(void *, const char *);
     56       1.22     joerg static int	radiodetach(device_t, int);
     57        1.1  augustss 
     58       1.22     joerg CFATTACH_DECL_NEW(radio, sizeof(struct radio_softc),
     59       1.24    dyoung     radioprobe, radioattach, radiodetach, NULL);
     60        1.1  augustss 
     61       1.22     joerg static dev_type_open(radioopen);
     62       1.22     joerg static dev_type_close(radioclose);
     63       1.22     joerg static dev_type_ioctl(radioioctl);
     64        1.4   gehenna 
     65        1.4   gehenna const struct cdevsw radio_cdevsw = {
     66  1.25.18.1     rmind 	.d_open = radioopen,
     67  1.25.18.1     rmind 	.d_close = radioclose,
     68  1.25.18.1     rmind 	.d_read = noread,
     69  1.25.18.1     rmind 	.d_write = nowrite,
     70  1.25.18.1     rmind 	.d_ioctl = radioioctl,
     71  1.25.18.1     rmind 	.d_stop = nostop,
     72  1.25.18.1     rmind 	.d_tty = notty,
     73  1.25.18.1     rmind 	.d_poll = nopoll,
     74  1.25.18.1     rmind 	.d_mmap = nommap,
     75  1.25.18.1     rmind 	.d_kqfilter = nokqfilter,
     76  1.25.18.1     rmind 	.d_flag = D_OTHER,
     77        1.4   gehenna };
     78        1.4   gehenna 
     79        1.1  augustss extern struct cfdriver radio_cd;
     80        1.1  augustss 
     81       1.22     joerg static int
     82       1.22     joerg radioprobe(device_t parent, cfdata_t match, void *aux)
     83        1.1  augustss {
     84        1.1  augustss 	return (1);
     85        1.1  augustss }
     86        1.1  augustss 
     87       1.22     joerg static void
     88       1.22     joerg radioattach(device_t parent, device_t self, void *aux)
     89        1.1  augustss {
     90       1.25    dyoung 	struct radio_softc *sc = device_private(self);
     91        1.1  augustss 	struct radio_attach_args *sa = aux;
     92       1.13      yamt 	const struct radio_hw_if *hwp = sa->hwif;
     93        1.1  augustss 	void  *hdlp = sa->hdl;
     94        1.1  augustss 
     95       1.22     joerg 	aprint_naive("\n");
     96       1.22     joerg 	aprint_normal("\n");
     97        1.1  augustss 	sc->hw_if = hwp;
     98        1.1  augustss 	sc->hw_hdl = hdlp;
     99       1.25    dyoung 	sc->sc_dev = self;
    100        1.1  augustss }
    101        1.1  augustss 
    102       1.22     joerg static int
    103       1.15  christos radioopen(dev_t dev, int flags, int fmt, struct lwp *l)
    104        1.1  augustss {
    105        1.1  augustss 	int	unit;
    106        1.1  augustss 	struct radio_softc *sc;
    107        1.1  augustss 
    108        1.1  augustss 	unit = RADIOUNIT(dev);
    109       1.21   tsutsui 	sc = device_lookup_private(&radio_cd, unit);
    110       1.21   tsutsui 	if (sc == NULL || sc->hw_if == NULL)
    111       1.14     perry 		return (ENXIO);
    112        1.1  augustss 
    113        1.1  augustss 	if (sc->hw_if->open != NULL)
    114       1.15  christos 		return (sc->hw_if->open(sc->hw_hdl, flags, fmt, l->l_proc));
    115        1.1  augustss 	else
    116        1.1  augustss 		return (0);
    117        1.1  augustss }
    118        1.1  augustss 
    119       1.22     joerg static int
    120       1.15  christos radioclose(dev_t dev, int flags, int fmt, struct lwp *l)
    121        1.1  augustss {
    122        1.1  augustss 	struct radio_softc *sc;
    123        1.1  augustss 
    124       1.21   tsutsui 	sc = device_lookup_private(&radio_cd, RADIOUNIT(dev));
    125        1.1  augustss 
    126        1.1  augustss 	if (sc->hw_if->close != NULL)
    127       1.15  christos 		return (sc->hw_if->close(sc->hw_hdl, flags, fmt, l->l_proc));
    128        1.1  augustss 	else
    129        1.1  augustss 		return (0);
    130        1.1  augustss }
    131        1.1  augustss 
    132       1.22     joerg static int
    133       1.20  christos radioioctl(dev_t dev, u_long cmd, void *data, int flags,
    134       1.19  christos     struct lwp *l)
    135        1.1  augustss {
    136        1.1  augustss 	struct radio_softc *sc;
    137        1.1  augustss 	int unit, error;
    138        1.1  augustss 
    139        1.1  augustss 	unit = RADIOUNIT(dev);
    140       1.21   tsutsui 	sc = device_lookup_private(&radio_cd, unit);
    141       1.21   tsutsui 	if (sc == NULL || sc->hw_if == NULL)
    142        1.1  augustss 		return (ENXIO);
    143        1.1  augustss 
    144        1.1  augustss 	error = EOPNOTSUPP;
    145        1.1  augustss 	switch (cmd) {
    146        1.1  augustss 	case RIOCGINFO:
    147        1.1  augustss 		if (sc->hw_if->get_info)
    148        1.1  augustss 			error = (sc->hw_if->get_info)(sc->hw_hdl,
    149        1.1  augustss 					(struct radio_info *)data);
    150        1.1  augustss 			break;
    151        1.1  augustss 	case RIOCSINFO:
    152        1.1  augustss 		if (sc->hw_if->set_info)
    153        1.1  augustss 			error = (sc->hw_if->set_info)(sc->hw_hdl,
    154        1.1  augustss 				(struct radio_info *)data);
    155        1.1  augustss 		break;
    156        1.1  augustss 	case RIOCSSRCH:
    157        1.1  augustss 		if (sc->hw_if->search)
    158        1.1  augustss 			error = (sc->hw_if->search)(sc->hw_hdl,
    159        1.1  augustss 					*(int *)data);
    160        1.1  augustss 		break;
    161        1.1  augustss 	default:
    162        1.1  augustss 		error = EINVAL;
    163        1.1  augustss 	}
    164        1.1  augustss 
    165        1.1  augustss 	return (error);
    166        1.1  augustss }
    167        1.1  augustss 
    168        1.1  augustss /*
    169        1.1  augustss  * Called from hardware driver. This is where the MI radio driver gets
    170        1.1  augustss  * probed/attached to the hardware driver
    171        1.1  augustss  */
    172       1.22     joerg device_t
    173       1.22     joerg radio_attach_mi(const struct radio_hw_if *rhwp, void *hdlp, device_t dev)
    174        1.1  augustss {
    175        1.1  augustss 	struct radio_attach_args arg;
    176        1.1  augustss 
    177        1.1  augustss 	arg.hwif = rhwp;
    178        1.1  augustss 	arg.hdl = hdlp;
    179        1.1  augustss 	return (config_found(dev, &arg, radioprint));
    180        1.1  augustss }
    181        1.1  augustss 
    182       1.22     joerg static int
    183       1.19  christos radioprint(void *aux, const char *pnp)
    184        1.1  augustss {
    185        1.2  augustss 	if (pnp != NULL)
    186        1.9   thorpej 		aprint_normal("radio at %s", pnp);
    187        1.2  augustss 	return (UNCONF);
    188        1.2  augustss }
    189        1.2  augustss 
    190       1.22     joerg static int
    191       1.22     joerg radiodetach(device_t self, int flags)
    192        1.2  augustss {
    193        1.2  augustss 	int maj, mn;
    194        1.2  augustss 
    195        1.2  augustss 	/* locate the major number */
    196        1.4   gehenna 	maj = cdevsw_lookup_major(&radio_cdevsw);
    197        1.2  augustss 
    198        1.2  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    199       1.16   thorpej 	mn = device_unit(self);
    200        1.2  augustss 	vdevgone(maj, mn, mn, VCHR);
    201        1.2  augustss 
    202        1.2  augustss 	return (0);
    203        1.2  augustss }
    204