irframe.c revision 1.2 1 /* $NetBSD: irframe.c,v 1.2 2001/12/02 16:29:25 augustss Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/ioctl.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/conf.h>
45 #include <sys/malloc.h>
46 #include <sys/poll.h>
47 #include <sys/select.h>
48 #include <sys/vnode.h>
49
50 #include <dev/ir/ir.h>
51 #include <dev/ir/irdaio.h>
52 #include <dev/ir/irframevar.h>
53
54 cdev_decl(irframe);
55
56 int irframe_match(struct device *parent, struct cfdata *match, void *aux);
57 void irframe_attach(struct device *parent, struct device *self, void *aux);
58 int irframe_activate(struct device *self, enum devact act);
59 int irframe_detach(struct device *self, int flags);
60
61 struct cfattach irframe_ca = {
62 sizeof(struct irframe_softc), irframe_match, irframe_attach,
63 irframe_detach, irframe_activate
64 };
65
66 extern struct cfattach irframe_ca;
67 extern struct cfdriver irframe_cd;
68
69 #define IRFRAMEUNIT(dev) (minor(dev))
70
71 int
72 irframe_match(struct device *parent, struct cfdata *match, void *aux)
73 {
74 struct ir_attach_args *ia = aux;
75
76 return (ia->ia_type == IR_TYPE_IRFRAME);
77 }
78
79 void
80 irframe_attach(struct device *parent, struct device *self, void *aux)
81 {
82 struct irframe_softc *sc = (struct irframe_softc *)self;
83 struct ir_attach_args *ia = aux;
84 const char *delim;
85 int speeds = 0;
86
87 sc->sc_methods = ia->ia_methods;
88 sc->sc_handle = ia->ia_handle;
89
90 #ifdef DIAGNOSTIC
91 if (sc->sc_methods->im_read == NULL ||
92 sc->sc_methods->im_write == NULL ||
93 sc->sc_methods->im_set_params == NULL ||
94 sc->sc_methods->im_reset_params == NULL ||
95 sc->sc_methods->im_get_speeds == NULL ||
96 sc->sc_methods->im_get_turnarounds == NULL)
97 panic("%s: missing methods\n", sc->sc_dev.dv_xname);
98 #endif
99
100 (void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
101 delim = ":";
102 if (speeds & IRDA_SPEEDS_SIR) {
103 printf("%s SIR", delim);
104 delim = ",";
105 }
106 if (speeds & IRDA_SPEEDS_MIR) {
107 printf("%s MIR", delim);
108 delim = ",";
109 }
110 if (speeds & IRDA_SPEEDS_FIR) {
111 printf("%s FIR", delim);
112 delim = ",";
113 }
114 if (speeds & IRDA_SPEEDS_VFIR) {
115 printf("%s VFIR", delim);
116 delim = ",";
117 }
118 printf("\n");
119 }
120
121 int
122 irframe_activate(struct device *self, enum devact act)
123 {
124 /*struct irframe_softc *sc = (struct irframe_softc *)self;*/
125
126 switch (act) {
127 case DVACT_ACTIVATE:
128 return (EOPNOTSUPP);
129 break;
130
131 case DVACT_DEACTIVATE:
132 break;
133 }
134 return (0);
135 }
136
137 int
138 irframe_detach(struct device *self, int flags)
139 {
140 /*struct irframe_softc *sc = (struct irframe_softc *)self;*/
141 int maj, mn;
142
143 /* locate the major number */
144 for (maj = 0; maj < nchrdev; maj++)
145 if (cdevsw[maj].d_open == irframeopen)
146 break;
147
148 /* Nuke the vnodes for any open instances (calls close). */
149 mn = self->dv_unit;
150 vdevgone(maj, mn, mn, VCHR);
151
152 return (0);
153 }
154
155 int
156 irframeopen(dev_t dev, int flag, int mode, struct proc *p)
157 {
158 struct irframe_softc *sc;
159 int error;
160
161 sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
162 if (sc == NULL)
163 return (ENXIO);
164 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
165 return (EIO);
166 if (sc->sc_open)
167 return (EBUSY);
168 if (sc->sc_methods->im_open != NULL) {
169 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p);
170 if (error)
171 return (error);
172 }
173 sc->sc_open = 1;
174 return (0);
175 }
176
177 int
178 irframeclose(dev_t dev, int flag, int mode, struct proc *p)
179 {
180 struct irframe_softc *sc;
181 int error;
182
183 sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
184 if (sc == NULL)
185 return (ENXIO);
186 if (sc->sc_methods->im_close != NULL)
187 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p);
188 else
189 error = 0;
190 sc->sc_open = 0;
191 return (error);
192 }
193
194 int
195 irframeread(dev_t dev, struct uio *uio, int flag)
196 {
197 struct irframe_softc *sc;
198
199 sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
200 if (sc == NULL)
201 return (ENXIO);
202 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
203 return (EIO);
204 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
205 }
206
207 int
208 irframewrite(dev_t dev, struct uio *uio, int flag)
209 {
210 struct irframe_softc *sc;
211
212 sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
213 if (sc == NULL)
214 return (ENXIO);
215 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
216 return (EIO);
217 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
218 }
219
220 int
221 irframeioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
222 {
223 struct irframe_softc *sc;
224 void *vaddr = addr;
225 int error;
226
227 sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
228 if (sc == NULL)
229 return (ENXIO);
230 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
231 return (EIO);
232
233 switch (cmd) {
234 case FIONBIO:
235 /* All handled in the upper FS layer. */
236 error = 0;
237 break;
238
239 case IRDA_SET_PARAMS:
240 error = sc->sc_methods->im_set_params(sc->sc_handle, vaddr);
241 break;
242
243 case IRDA_RESET_PARAMS:
244 error = sc->sc_methods->im_reset_params(sc->sc_handle);
245 break;
246
247 case IRDA_GET_SPEEDMASK:
248 error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
249 break;
250
251 case IRDA_GET_TURNAROUNDMASK:
252 error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
253 break;
254
255 default:
256 error = EINVAL;
257 break;
258 }
259 return (error);
260 }
261
262 int
263 irframepoll(dev_t dev, int events, struct proc *p)
264 {
265 struct irframe_softc *sc;
266
267 sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
268 if (sc == NULL)
269 return (ENXIO);
270 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
271 return (EIO);
272
273 if (sc->sc_methods->im_poll != NULL)
274 return (sc->sc_methods->im_poll(sc->sc_handle, events, p));
275 else
276 return (0);
277 }
278
279 void
280 irframe_frame_available(struct device *dev)
281 {
282 struct irframe_softc *sc = (struct irframe_softc *)dev;
283 int s;
284
285 s = splir();
286 sc->sc_rdframes++;
287 selwakeup(&sc->sc_rdsel);
288 splx(s);
289 }
290
291 /*********/
292
293
294 struct device *
295 irframe_alloc(size_t size, struct irframe_methods *m, void *h)
296 {
297 struct cfdriver *cd = &irframe_cd;
298 struct device *dev;
299 int unit;
300
301 for (unit = 0; unit < cd->cd_ndevs; unit++)
302 if (cd->cd_devs[unit] == NULL)
303 break;
304 dev = malloc(size, M_DEVBUF, M_WAITOK);
305 memset(dev, 0, size);
306 snprintf(dev->dv_xname, sizeof dev->dv_xname, "irframe%d", unit);
307 dev->dv_unit = unit;
308 dev->dv_flags = DVF_ACTIVE; /* always initially active */
309
310 config_makeroom(unit, cd);
311 cd->cd_devs[unit] = dev;
312
313 return (dev);
314 }
315
316 void
317 irframe_dealloc(struct device *dev)
318 {
319 struct cfdriver *cd = &irframe_cd;
320 int unit;
321
322 for (unit = 0; unit < cd->cd_ndevs; unit++) {
323 if (cd->cd_devs[unit] == dev) {
324 cd->cd_devs[unit] = NULL;
325 free(dev, M_DEVBUF);
326 return;
327 }
328 }
329 panic("irframe_dealloc: device not found\n");
330 }
331