xref: /netbsd-src/sys/dev/radio.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: radio.c,v 1.24 2010/01/21 02:19:55 dyoung 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.24 2010/01/21 02:19:55 dyoung 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 	radioopen, radioclose, noread, nowrite, radioioctl,
67 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
68 };
69 
70 extern struct cfdriver radio_cd;
71 
72 static int
73 radioprobe(device_t parent, cfdata_t match, void *aux)
74 {
75 	return (1);
76 }
77 
78 static void
79 radioattach(device_t parent, device_t self, void *aux)
80 {
81 	struct radio_softc *sc = (void *)self;
82 	struct radio_attach_args *sa = aux;
83 	const struct radio_hw_if *hwp = sa->hwif;
84 	void  *hdlp = sa->hdl;
85 
86 	aprint_naive("\n");
87 	aprint_normal("\n");
88 	sc->hw_if = hwp;
89 	sc->hw_hdl = hdlp;
90 	sc->sc_dev = parent;
91 }
92 
93 static int
94 radioopen(dev_t dev, int flags, int fmt, struct lwp *l)
95 {
96 	int	unit;
97 	struct radio_softc *sc;
98 
99 	unit = RADIOUNIT(dev);
100 	sc = device_lookup_private(&radio_cd, unit);
101 	if (sc == NULL || sc->hw_if == NULL)
102 		return (ENXIO);
103 
104 	if (sc->hw_if->open != NULL)
105 		return (sc->hw_if->open(sc->hw_hdl, flags, fmt, l->l_proc));
106 	else
107 		return (0);
108 }
109 
110 static int
111 radioclose(dev_t dev, int flags, int fmt, struct lwp *l)
112 {
113 	struct radio_softc *sc;
114 
115 	sc = device_lookup_private(&radio_cd, RADIOUNIT(dev));
116 
117 	if (sc->hw_if->close != NULL)
118 		return (sc->hw_if->close(sc->hw_hdl, flags, fmt, l->l_proc));
119 	else
120 		return (0);
121 }
122 
123 static int
124 radioioctl(dev_t dev, u_long cmd, void *data, int flags,
125     struct lwp *l)
126 {
127 	struct radio_softc *sc;
128 	int unit, error;
129 
130 	unit = RADIOUNIT(dev);
131 	sc = device_lookup_private(&radio_cd, unit);
132 	if (sc == NULL || sc->hw_if == NULL)
133 		return (ENXIO);
134 
135 	error = EOPNOTSUPP;
136 	switch (cmd) {
137 	case RIOCGINFO:
138 		if (sc->hw_if->get_info)
139 			error = (sc->hw_if->get_info)(sc->hw_hdl,
140 					(struct radio_info *)data);
141 			break;
142 	case RIOCSINFO:
143 		if (sc->hw_if->set_info)
144 			error = (sc->hw_if->set_info)(sc->hw_hdl,
145 				(struct radio_info *)data);
146 		break;
147 	case RIOCSSRCH:
148 		if (sc->hw_if->search)
149 			error = (sc->hw_if->search)(sc->hw_hdl,
150 					*(int *)data);
151 		break;
152 	default:
153 		error = EINVAL;
154 	}
155 
156 	return (error);
157 }
158 
159 /*
160  * Called from hardware driver. This is where the MI radio driver gets
161  * probed/attached to the hardware driver
162  */
163 device_t
164 radio_attach_mi(const struct radio_hw_if *rhwp, void *hdlp, device_t dev)
165 {
166 	struct radio_attach_args arg;
167 
168 	arg.hwif = rhwp;
169 	arg.hdl = hdlp;
170 	return (config_found(dev, &arg, radioprint));
171 }
172 
173 static int
174 radioprint(void *aux, const char *pnp)
175 {
176 	if (pnp != NULL)
177 		aprint_normal("radio at %s", pnp);
178 	return (UNCONF);
179 }
180 
181 static int
182 radiodetach(device_t self, int flags)
183 {
184 	/*struct radio_softc *sc = (struct radio_softc *)self;*/
185 	int maj, mn;
186 
187 	/* locate the major number */
188 	maj = cdevsw_lookup_major(&radio_cdevsw);
189 
190 	/* Nuke the vnodes for any open instances (calls close). */
191 	mn = device_unit(self);
192 	vdevgone(maj, mn, mn, VCHR);
193 
194 	return (0);
195 }
196