1/* $OpenBSD: radio.c,v 1.13 2022/04/06 18:59:27 naddy Exp $ */
2/* $RuOBSD: radio.c,v 1.7 2001/12/04 06:03:05 tm Exp $ */
3
4/*
5 * Copyright (c) 2001 Maxim Tsyplakov <tm@oganer.net>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/* This is /dev/radio driver for OpenBSD */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/errno.h>
34#include <sys/ioctl.h>
35#include <sys/fcntl.h>
36#include <sys/device.h>
37#include <sys/vnode.h>
38#include <sys/radioio.h>
39#include <sys/conf.h>
40
41#include <dev/audio_if.h>
42#include <dev/radio_if.h>
43#include <dev/radiovar.h>
44
45int	radioprobe(struct device *, void *, void *);
46void	radioattach(struct device *, struct device *, void *);
47int	radiodetach(struct device *, int);
48int	radioactivate(struct device *, int);
49int	radioprint(void *, const char *);
50
51const struct cfattach radio_ca = {
52	sizeof(struct radio_softc), radioprobe, radioattach,
53	radiodetach, radioactivate
54};
55
56struct cfdriver radio_cd = {
57	NULL, "radio", DV_DULL
58};
59
60int
61radioprobe(struct device *parent, void *match, void *aux)
62{
63	struct audio_attach_args *sa = aux;
64	return (sa->type == AUDIODEV_TYPE_RADIO) ? 1 : 0;
65}
66
67void
68radioattach(struct device *parent, struct device *self, void *aux)
69{
70	struct radio_softc *sc = (void *) self;
71	struct audio_attach_args *sa = aux;
72
73	printf("\n");
74	sc->hw_if = sa->hwif;
75	sc->hw_hdl = sa->hdl;
76	sc->sc_dev = parent;
77}
78
79int
80radioopen(dev_t dev, int flags, int fmt, struct proc *p)
81{
82	int	unit;
83	struct radio_softc *sc;
84
85	unit = RADIOUNIT(dev);
86	if (unit >= radio_cd.cd_ndevs ||
87	    (sc = radio_cd.cd_devs[unit]) == NULL ||
88	     sc->hw_if == NULL)
89		return (ENXIO);
90
91	if (sc->hw_if->open != NULL)
92		return (sc->hw_if->open(sc->hw_hdl, flags, fmt, p));
93	else
94		return (0);
95}
96
97int
98radioclose(dev_t dev, int flags, int fmt, struct proc *p)
99{
100	struct radio_softc *sc;
101
102	sc = radio_cd.cd_devs[RADIOUNIT(dev)];
103
104	if (sc->hw_if->close != NULL)
105		return (sc->hw_if->close(sc->hw_hdl, flags, fmt, p));
106	else
107		return (0);
108}
109
110int
111radioioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
112{
113	struct radio_softc *sc;
114	int unit, error;
115
116	unit = RADIOUNIT(dev);
117	if (unit >= radio_cd.cd_ndevs ||
118	    (sc = radio_cd.cd_devs[unit]) == NULL || sc->hw_if == NULL)
119		return (ENXIO);
120
121	error = EOPNOTSUPP;
122	switch (cmd) {
123	case RIOCGINFO:
124		if (sc->hw_if->get_info)
125			error = (sc->hw_if->get_info)(sc->hw_hdl,
126					(struct radio_info *)data);
127		break;
128	case RIOCSINFO:
129		if (!(flags & FWRITE))
130			return (EACCES);
131		if (sc->hw_if->set_info)
132			error = (sc->hw_if->set_info)(sc->hw_hdl,
133				(struct radio_info *)data);
134		break;
135	case RIOCSSRCH:
136		if (!(flags & FWRITE))
137			return (EACCES);
138		if (sc->hw_if->search)
139			error = (sc->hw_if->search)(sc->hw_hdl,
140					*(int *)data);
141		break;
142	default:
143		error = (ENOTTY);
144	}
145
146	return (error);
147}
148
149/*
150 * Called from hardware driver. This is where the MI radio driver gets
151 * probed/attached to the hardware driver
152 */
153
154struct device *
155radio_attach_mi(const struct radio_hw_if *rhwp, void *hdlp, struct device *dev)
156{
157	struct audio_attach_args arg;
158
159	arg.type = AUDIODEV_TYPE_RADIO;
160	arg.hwif = rhwp;
161	arg.hdl = hdlp;
162	return (config_found(dev, &arg, radioprint));
163}
164
165int
166radioprint(void *aux, const char *pnp)
167{
168	if (pnp != NULL)
169		printf("radio at %s", pnp);
170	return (UNCONF);
171}
172
173int
174radiodetach(struct device *self, int flags)
175{
176	/*struct radio_softc *sc = (struct radio_softc *)self;*/
177	int maj, mn;
178
179	/* locate the major number */
180	for (maj = 0; maj < nchrdev; maj++)
181		if (cdevsw[maj].d_open == radioopen)
182			break;
183
184	/* Nuke the vnodes for any open instances (calls close). */
185	mn = self->dv_unit;
186	vdevgone(maj, mn, mn, VCHR);
187
188	return (0);
189}
190
191int
192radioactivate(struct device *self, int act)
193{
194	struct radio_softc *sc = (struct radio_softc *)self;
195
196	switch (act) {
197	case DVACT_DEACTIVATE:
198		sc->sc_dying = 1;
199		break;
200	}
201	return (0);
202}
203