xref: /netbsd-src/sys/dev/ir/cir.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: cir.c,v 1.29 2011/07/26 08:59:38 mrg 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.29 2011/07/26 08:59:38 mrg 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_NEW(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 	sc->sc_dev = self;
89 
90 	selinit(&sc->sc_rdsel);
91 	sc->sc_methods = ia->ia_methods;
92 	sc->sc_handle = ia->ia_handle;
93 
94 #ifdef DIAGNOSTIC
95 	if (sc->sc_methods->im_read == NULL ||
96 	    sc->sc_methods->im_write == NULL ||
97 	    sc->sc_methods->im_setparams == NULL)
98 		panic("%s: missing methods", device_xname(sc->sc_dev));
99 #endif
100 	printf("\n");
101 }
102 
103 int
104 cir_detach(device_t self, int flags)
105 {
106 	struct cir_softc *sc = device_private(self);
107 	int maj, mn;
108 
109 	/* locate the major number */
110 	maj = cdevsw_lookup_major(&cir_cdevsw);
111 
112 	/* Nuke the vnodes for any open instances (calls close). */
113 	mn = device_unit(self);
114 	vdevgone(maj, mn, mn, VCHR);
115 
116 	seldestroy(&sc->sc_rdsel);
117 
118 	return (0);
119 }
120 
121 int
122 ciropen(dev_t dev, int flag, int mode, struct lwp *l)
123 {
124 	struct cir_softc *sc;
125 	int error;
126 
127 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
128 	if (sc == NULL)
129 		return (ENXIO);
130 	if (!device_is_active(sc->sc_dev))
131 		return (EIO);
132 	if (sc->sc_open)
133 		return (EBUSY);
134 
135 	sc->sc_rdframes = 0;
136 	if (sc->sc_methods->im_open != NULL) {
137 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
138 		    l->l_proc);
139 		if (error)
140 			return (error);
141 	}
142 	sc->sc_open = 1;
143 	return (0);
144 }
145 
146 int
147 circlose(dev_t dev, int flag, int mode, struct lwp *l)
148 {
149 	struct cir_softc *sc;
150 	int error;
151 
152 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
153 	if (sc == NULL)
154 		return (ENXIO);
155 	if (sc->sc_methods->im_close != NULL)
156 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
157 		    l->l_proc);
158 	else
159 		error = 0;
160 	sc->sc_open = 0;
161 	return (error);
162 }
163 
164 int
165 cirread(dev_t dev, struct uio *uio, int flag)
166 {
167 	struct cir_softc *sc;
168 
169 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
170 	if (sc == NULL)
171 		return (ENXIO);
172 	if (!device_is_active(sc->sc_dev))
173 		return (EIO);
174 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
175 }
176 
177 int
178 cirwrite(dev_t dev, struct uio *uio, int flag)
179 {
180 	struct cir_softc *sc;
181 
182 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
183 	if (sc == NULL)
184 		return (ENXIO);
185 	if (!device_is_active(sc->sc_dev))
186 		return (EIO);
187 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
188 }
189 
190 int
191 cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
192 {
193 	struct cir_softc *sc;
194 	int error;
195 
196 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
197 	if (sc == NULL)
198 		return (ENXIO);
199 	if (!device_is_active(sc->sc_dev))
200 		return (EIO);
201 
202 	switch (cmd) {
203 	case FIONBIO:
204 		/* All handled in the upper FS layer. */
205 		error = 0;
206 		break;
207 	case CIR_GET_PARAMS:
208 		*(struct cir_params *)addr = sc->sc_params;
209 		error = 0;
210 		break;
211 	case CIR_SET_PARAMS:
212 		error = sc->sc_methods->im_setparams(sc->sc_handle,
213 			    (struct cir_params *)addr);
214 		if (!error)
215 			sc->sc_params = *(struct cir_params *)addr;
216 		break;
217 	default:
218 		error = EINVAL;
219 		break;
220 	}
221 	return (error);
222 }
223 
224 int
225 cirpoll(dev_t dev, int events, struct lwp *l)
226 {
227 	struct cir_softc *sc;
228 	int revents;
229 	int s;
230 
231 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
232 	if (sc == NULL)
233 		return (POLLERR);
234 	if (!device_is_active(sc->sc_dev))
235 		return (POLLERR);
236 
237 	revents = 0;
238 	s = splir();
239 	if (events & (POLLIN | POLLRDNORM))
240 		if (sc->sc_rdframes > 0)
241 			revents |= events & (POLLIN | POLLRDNORM);
242 
243 #if 0
244 	/* How about write? */
245 	if (events & (POLLOUT | POLLWRNORM))
246 		if (/* ??? */)
247 			revents |= events & (POLLOUT | POLLWRNORM);
248 #endif
249 
250 	if (revents == 0) {
251 		if (events & (POLLIN | POLLRDNORM))
252 			selrecord(l, &sc->sc_rdsel);
253 
254 #if 0
255 		if (events & (POLLOUT | POLLWRNORM))
256 			selrecord(p, &sc->sc_wrsel);
257 #endif
258 	}
259 
260 	splx(s);
261 	return (revents);
262 }
263 
264 MODULE(MODULE_CLASS_DRIVER, cir, "ir");
265 
266 #ifdef _MODULE
267 #include "ioconf.c"
268 #endif
269 
270 static int
271 cir_modcmd(modcmd_t cmd, void *opaque)
272 {
273 	int error = 0;
274 #ifdef _MODULE
275 	int bmaj = -1, cmaj = -1;
276 #endif
277 
278 	switch (cmd) {
279 	case MODULE_CMD_INIT:
280 #ifdef _MODULE
281 		error = config_init_component(cfdriver_ioconf_cir,
282 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
283 		if (error)
284 			return error;
285 		error = devsw_attach("cir", NULL, &bmaj, &cir_cdevsw, &cmaj);
286 		if (error)
287 			config_fini_component(cfdriver_ioconf_cir,
288 			    cfattach_ioconf_cir, cfdata_ioconf_cir);
289 #endif
290 		return error;
291 	case MODULE_CMD_FINI:
292 #ifdef _MODULE
293 		devsw_detach(NULL, &cir_cdevsw);
294 		return config_fini_component(cfdriver_ioconf_cir,
295 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
296 #endif
297 		return error;
298 	default:
299 		return ENOTTY;
300 	}
301 }
302