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