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