xref: /netbsd-src/sys/dev/ir/cir.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: cir.c,v 1.28 2010/12/29 13:43:16 jmcneill 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@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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: cir.c,v 1.28 2010/12/29 13:43:16 jmcneill Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ioctl.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/poll.h>
42 #include <sys/select.h>
43 #include <sys/vnode.h>
44 #include <sys/module.h>
45 
46 #include <dev/ir/ir.h>
47 #include <dev/ir/cirio.h>
48 #include <dev/ir/cirvar.h>
49 
50 dev_type_open(ciropen);
51 dev_type_close(circlose);
52 dev_type_read(cirread);
53 dev_type_write(cirwrite);
54 dev_type_ioctl(cirioctl);
55 dev_type_poll(cirpoll);
56 
57 const struct cdevsw cir_cdevsw = {
58 	ciropen, circlose, cirread, cirwrite, cirioctl,
59 	nostop, notty, cirpoll, nommap, nokqfilter,
60 	D_OTHER
61 };
62 
63 int cir_match(device_t parent, cfdata_t match, void *aux);
64 void cir_attach(device_t parent, device_t self, void *aux);
65 int cir_detach(device_t self, int flags);
66 
67 CFATTACH_DECL(cir, sizeof(struct cir_softc),
68     cir_match, cir_attach, cir_detach, NULL);
69 
70 extern struct cfdriver cir_cd;
71 
72 #define CIRUNIT(dev) (minor(dev))
73 
74 int
75 cir_match(device_t parent, cfdata_t match, void *aux)
76 {
77 	struct ir_attach_args *ia = aux;
78 
79 	return (ia->ia_type == IR_TYPE_CIR);
80 }
81 
82 void
83 cir_attach(device_t parent, device_t self, void *aux)
84 {
85 	struct cir_softc *sc = device_private(self);
86 	struct ir_attach_args *ia = aux;
87 
88 	selinit(&sc->sc_rdsel);
89 	sc->sc_methods = ia->ia_methods;
90 	sc->sc_handle = ia->ia_handle;
91 
92 #ifdef DIAGNOSTIC
93 	if (sc->sc_methods->im_read == NULL ||
94 	    sc->sc_methods->im_write == NULL ||
95 	    sc->sc_methods->im_setparams == NULL)
96 		panic("%s: missing methods", device_xname(&sc->sc_dev));
97 #endif
98 	printf("\n");
99 }
100 
101 int
102 cir_detach(device_t self, int flags)
103 {
104 	struct cir_softc *sc = device_private(self);
105 	int maj, mn;
106 
107 	/* locate the major number */
108 	maj = cdevsw_lookup_major(&cir_cdevsw);
109 
110 	/* Nuke the vnodes for any open instances (calls close). */
111 	mn = device_unit(self);
112 	vdevgone(maj, mn, mn, VCHR);
113 
114 	seldestroy(&sc->sc_rdsel);
115 
116 	return (0);
117 }
118 
119 int
120 ciropen(dev_t dev, int flag, int mode, struct lwp *l)
121 {
122 	struct cir_softc *sc;
123 	int error;
124 
125 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
126 	if (sc == NULL)
127 		return (ENXIO);
128 	if (!device_is_active(&sc->sc_dev))
129 		return (EIO);
130 	if (sc->sc_open)
131 		return (EBUSY);
132 
133 	sc->sc_rdframes = 0;
134 	if (sc->sc_methods->im_open != NULL) {
135 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
136 		    l->l_proc);
137 		if (error)
138 			return (error);
139 	}
140 	sc->sc_open = 1;
141 	return (0);
142 }
143 
144 int
145 circlose(dev_t dev, int flag, int mode, struct lwp *l)
146 {
147 	struct cir_softc *sc;
148 	int error;
149 
150 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
151 	if (sc == NULL)
152 		return (ENXIO);
153 	if (sc->sc_methods->im_close != NULL)
154 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
155 		    l->l_proc);
156 	else
157 		error = 0;
158 	sc->sc_open = 0;
159 	return (error);
160 }
161 
162 int
163 cirread(dev_t dev, struct uio *uio, int flag)
164 {
165 	struct cir_softc *sc;
166 
167 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
168 	if (sc == NULL)
169 		return (ENXIO);
170 	if (!device_is_active(&sc->sc_dev))
171 		return (EIO);
172 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
173 }
174 
175 int
176 cirwrite(dev_t dev, struct uio *uio, int flag)
177 {
178 	struct cir_softc *sc;
179 
180 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
181 	if (sc == NULL)
182 		return (ENXIO);
183 	if (!device_is_active(&sc->sc_dev))
184 		return (EIO);
185 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
186 }
187 
188 int
189 cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
190 {
191 	struct cir_softc *sc;
192 	int error;
193 
194 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
195 	if (sc == NULL)
196 		return (ENXIO);
197 	if (!device_is_active(&sc->sc_dev))
198 		return (EIO);
199 
200 	switch (cmd) {
201 	case FIONBIO:
202 		/* All handled in the upper FS layer. */
203 		error = 0;
204 		break;
205 	case CIR_GET_PARAMS:
206 		*(struct cir_params *)addr = sc->sc_params;
207 		error = 0;
208 		break;
209 	case CIR_SET_PARAMS:
210 		error = sc->sc_methods->im_setparams(sc->sc_handle,
211 			    (struct cir_params *)addr);
212 		if (!error)
213 			sc->sc_params = *(struct cir_params *)addr;
214 		break;
215 	default:
216 		error = EINVAL;
217 		break;
218 	}
219 	return (error);
220 }
221 
222 int
223 cirpoll(dev_t dev, int events, struct lwp *l)
224 {
225 	struct cir_softc *sc;
226 	int revents;
227 	int s;
228 
229 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
230 	if (sc == NULL)
231 		return (POLLERR);
232 	if (!device_is_active(&sc->sc_dev))
233 		return (POLLERR);
234 
235 	revents = 0;
236 	s = splir();
237 	if (events & (POLLIN | POLLRDNORM))
238 		if (sc->sc_rdframes > 0)
239 			revents |= events & (POLLIN | POLLRDNORM);
240 
241 #if 0
242 	/* How about write? */
243 	if (events & (POLLOUT | POLLWRNORM))
244 		if (/* ??? */)
245 			revents |= events & (POLLOUT | POLLWRNORM);
246 #endif
247 
248 	if (revents == 0) {
249 		if (events & (POLLIN | POLLRDNORM))
250 			selrecord(l, &sc->sc_rdsel);
251 
252 #if 0
253 		if (events & (POLLOUT | POLLWRNORM))
254 			selrecord(p, &sc->sc_wrsel);
255 #endif
256 	}
257 
258 	splx(s);
259 	return (revents);
260 }
261 
262 MODULE(MODULE_CLASS_DRIVER, cir, "ir");
263 
264 #ifdef _MODULE
265 #include "ioconf.c"
266 #endif
267 
268 static int
269 cir_modcmd(modcmd_t cmd, void *opaque)
270 {
271 	int error = 0;
272 #ifdef _MODULE
273 	int bmaj = -1, cmaj = -1;
274 #endif
275 
276 	switch (cmd) {
277 	case MODULE_CMD_INIT:
278 #ifdef _MODULE
279 		error = config_init_component(cfdriver_ioconf_cir,
280 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
281 		if (error)
282 			return error;
283 		error = devsw_attach("cir", NULL, &bmaj, &cir_cdevsw, &cmaj);
284 		if (error)
285 			config_fini_component(cfdriver_ioconf_cir,
286 			    cfattach_ioconf_cir, cfdata_ioconf_cir);
287 #endif
288 		return error;
289 	case MODULE_CMD_FINI:
290 #ifdef _MODULE
291 		devsw_detach(NULL, &cir_cdevsw);
292 		return config_fini_component(cfdriver_ioconf_cir,
293 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
294 #endif
295 		return error;
296 	default:
297 		return ENOTTY;
298 	}
299 }
300