xref: /netbsd-src/sys/dev/radio.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /* $NetBSD: radio.c,v 1.31 2021/08/07 16:19:08 thorpej 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.31 2021/08/07 16:19:08 thorpej 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 #include "ioconf.h"
48 
49 struct radio_softc {
50 	void		*hw_hdl;	/* hardware driver handle */
51 	device_t 	sc_dev;		/* hardware device struct */
52 	const struct radio_hw_if *hw_if; /* hardware interface */
53 };
54 
55 static int	radioprobe(device_t, cfdata_t, void *);
56 static void	radioattach(device_t, device_t, void *);
57 static int	radioprint(void *, const char *);
58 static int	radiodetach(device_t, int);
59 
60 CFATTACH_DECL_NEW(radio, sizeof(struct radio_softc),
61     radioprobe, radioattach, radiodetach, NULL);
62 
63 static dev_type_open(radioopen);
64 static dev_type_close(radioclose);
65 static dev_type_ioctl(radioioctl);
66 
67 const struct cdevsw radio_cdevsw = {
68 	.d_open = radioopen,
69 	.d_close = radioclose,
70 	.d_read = noread,
71 	.d_write = nowrite,
72 	.d_ioctl = radioioctl,
73 	.d_stop = nostop,
74 	.d_tty = notty,
75 	.d_poll = nopoll,
76 	.d_mmap = nommap,
77 	.d_kqfilter = nokqfilter,
78 	.d_discard = nodiscard,
79 	.d_flag = D_OTHER,
80 };
81 
82 static int
radioprobe(device_t parent,cfdata_t match,void * aux)83 radioprobe(device_t parent, cfdata_t match, void *aux)
84 {
85 	return (1);
86 }
87 
88 static void
radioattach(device_t parent,device_t self,void * aux)89 radioattach(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 
103 static int
radioopen(dev_t dev,int flags,int fmt,struct lwp * l)104 radioopen(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 
120 static int
radioclose(dev_t dev,int flags,int fmt,struct lwp * l)121 radioclose(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 
133 static int
radioioctl(dev_t dev,u_long cmd,void * data,int flags,struct lwp * l)134 radioioctl(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  */
173 device_t
radio_attach_mi(const struct radio_hw_if * rhwp,void * hdlp,device_t dev)174 radio_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, CFARGS_NONE));
181 }
182 
183 static int
radioprint(void * aux,const char * pnp)184 radioprint(void *aux, const char *pnp)
185 {
186 	if (pnp != NULL)
187 		aprint_normal("radio at %s", pnp);
188 	return (UNCONF);
189 }
190 
191 static int
radiodetach(device_t self,int flags)192 radiodetach(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