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
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	radioopen, radioclose, noread, nowrite, radioioctl,
67	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
68};
69
70extern struct cfdriver radio_cd;
71
72static int
73radioprobe(device_t parent, cfdata_t match, void *aux)
74{
75	return (1);
76}
77
78static void
79radioattach(device_t parent, device_t self, void *aux)
80{
81	struct radio_softc *sc = device_private(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 = self;
91}
92
93static int
94radioopen(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
110static int
111radioclose(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
123static int
124radioioctl(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 */
163device_t
164radio_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
173static int
174radioprint(void *aux, const char *pnp)
175{
176	if (pnp != NULL)
177		aprint_normal("radio at %s", pnp);
178	return (UNCONF);
179}
180
181static int
182radiodetach(device_t self, int flags)
183{
184	int maj, mn;
185
186	/* locate the major number */
187	maj = cdevsw_lookup_major(&radio_cdevsw);
188
189	/* Nuke the vnodes for any open instances (calls close). */
190	mn = device_unit(self);
191	vdevgone(maj, mn, mn, VCHR);
192
193	return (0);
194}
195