radio.c revision 1.1 1 /* $NetBSD: radio.c,v 1.1 2002/01/01 21:51:38 augustss 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 (at) 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/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
35 #include <sys/errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/device.h>
38 #include <sys/radioio.h>
39
40 #include <dev/radio_if.h>
41 #include <dev/radiovar.h>
42
43 int radioprobe(struct device *, struct cfdata *, void *);
44 void radioattach(struct device *, struct device *, void *);
45 int radioopen(dev_t, int, int, struct proc *);
46 int radioclose(dev_t, int, int, struct proc *);
47 int radioioctl(dev_t, u_long, caddr_t, int, struct proc *);
48 int radioprint(void *, const char *);
49
50 struct cfattach radio_ca = {
51 sizeof(struct radio_softc), radioprobe, radioattach,
52 NULL, NULL
53 };
54
55 extern struct cfdriver radio_cd;
56
57 int
58 radioprobe(struct device *parent, struct cfdata *match, void *aux)
59 {
60 return (1);
61 }
62
63 void
64 radioattach(struct device *parent, struct device *self, void *aux)
65 {
66 struct radio_softc *sc = (void *)self;
67 struct radio_attach_args *sa = aux;
68 struct radio_hw_if *hwp = sa->hwif;
69 void *hdlp = sa->hdl;
70
71 printf("\n");
72 sc->hw_if = hwp;
73 sc->hw_hdl = hdlp;
74 sc->sc_dev = parent;
75 }
76
77 int
78 radioopen(dev_t dev, int flags, int fmt, struct proc *p)
79 {
80 int unit;
81 struct radio_softc *sc;
82
83 unit = RADIOUNIT(dev);
84 if (unit >= radio_cd.cd_ndevs ||
85 (sc = radio_cd.cd_devs[unit]) == NULL ||
86 sc->hw_if == NULL)
87 return (ENXIO);
88
89 if (sc->hw_if->open != NULL)
90 return (sc->hw_if->open(sc->hw_hdl, flags, fmt, p));
91 else
92 return (0);
93 }
94
95 int
96 radioclose(dev_t dev, int flags, int fmt, struct proc *p)
97 {
98 struct radio_softc *sc;
99
100 sc = radio_cd.cd_devs[RADIOUNIT(dev)];
101
102 if (sc->hw_if->close != NULL)
103 return (sc->hw_if->close(sc->hw_hdl, flags, fmt, p));
104 else
105 return (0);
106 }
107
108 int
109 radioioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
110 {
111 struct radio_softc *sc;
112 int unit, error;
113
114 unit = RADIOUNIT(dev);
115 if (unit >= radio_cd.cd_ndevs ||
116 (sc = radio_cd.cd_devs[unit]) == NULL || sc->hw_if == NULL)
117 return (ENXIO);
118
119 error = EOPNOTSUPP;
120 switch (cmd) {
121 case RIOCGINFO:
122 if (sc->hw_if->get_info)
123 error = (sc->hw_if->get_info)(sc->hw_hdl,
124 (struct radio_info *)data);
125 break;
126 case RIOCSINFO:
127 if (sc->hw_if->set_info)
128 error = (sc->hw_if->set_info)(sc->hw_hdl,
129 (struct radio_info *)data);
130 break;
131 case RIOCSSRCH:
132 if (sc->hw_if->search)
133 error = (sc->hw_if->search)(sc->hw_hdl,
134 *(int *)data);
135 break;
136 default:
137 error = EINVAL;
138 }
139
140 return (error);
141 }
142
143 /*
144 * Called from hardware driver. This is where the MI radio driver gets
145 * probed/attached to the hardware driver
146 */
147 struct device *
148 radio_attach_mi(struct radio_hw_if *rhwp, void *hdlp, struct device *dev)
149 {
150 struct radio_attach_args arg;
151
152 arg.hwif = rhwp;
153 arg.hdl = hdlp;
154 return (config_found(dev, &arg, radioprint));
155 }
156
157 int
158 radioprint(void *aux, const char *pnp)
159 {
160 return UNCONF;
161 }
162