xref: /netbsd/sys/dev/ir/cir.c (revision c4a72b64)
1 /*	$NetBSD: cir.c,v 1.8 2002/10/23 09:13:20 jdolecek 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  * 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/poll.h>
46 #include <sys/select.h>
47 #include <sys/vnode.h>
48 
49 #include <dev/ir/ir.h>
50 #include <dev/ir/cirio.h>
51 #include <dev/ir/cirvar.h>
52 
53 dev_type_open(ciropen);
54 dev_type_close(circlose);
55 dev_type_read(cirread);
56 dev_type_write(cirwrite);
57 dev_type_ioctl(cirioctl);
58 dev_type_poll(cirpoll);
59 dev_type_kqfilter(cirkqfilter);
60 
61 const struct cdevsw cir_cdevsw = {
62 	ciropen, circlose, cirread, cirwrite, cirioctl,
63 	nostop, notty, cirpoll, nommap, cirkqfilter,
64 };
65 
66 int cir_match(struct device *parent, struct cfdata *match, void *aux);
67 void cir_attach(struct device *parent, struct device *self, void *aux);
68 int cir_activate(struct device *self, enum devact act);
69 int cir_detach(struct device *self, int flags);
70 
71 CFATTACH_DECL(cir, sizeof(struct cir_softc),
72     cir_match, cir_attach, cir_detach, cir_activate);
73 
74 extern struct cfdriver cir_cd;
75 
76 #define CIRUNIT(dev) (minor(dev))
77 
78 int
79 cir_match(struct device *parent, struct cfdata *match, void *aux)
80 {
81 	struct ir_attach_args *ia = aux;
82 
83 	return (ia->ia_type == IR_TYPE_CIR);
84 }
85 
86 void
87 cir_attach(struct device *parent, struct device *self, void *aux)
88 {
89 	struct cir_softc *sc = (struct cir_softc *)self;
90 	struct ir_attach_args *ia = aux;
91 
92 	sc->sc_methods = ia->ia_methods;
93 	sc->sc_handle = ia->ia_handle;
94 
95 #ifdef DIAGNOSTIC
96 	if (sc->sc_methods->im_read == NULL ||
97 	    sc->sc_methods->im_write == NULL ||
98 	    sc->sc_methods->im_setparams == NULL)
99 		panic("%s: missing methods", sc->sc_dev.dv_xname);
100 #endif
101 	printf("\n");
102 }
103 
104 int
105 cir_activate(struct device *self, enum devact act)
106 {
107 	/*struct cir_softc *sc = (struct cir_softc *)self;*/
108 
109 	switch (act) {
110 	case DVACT_ACTIVATE:
111 		return (EOPNOTSUPP);
112 		break;
113 
114 	case DVACT_DEACTIVATE:
115 		break;
116 	}
117 	return (0);
118 }
119 
120 int
121 cir_detach(struct device *self, int flags)
122 {
123 	/*struct cir_softc *sc = (struct cir_softc *)self;*/
124 	int maj, mn;
125 
126 	/* locate the major number */
127 	maj = cdevsw_lookup_major(&cir_cdevsw);
128 
129 	/* Nuke the vnodes for any open instances (calls close). */
130 	mn = self->dv_unit;
131 	vdevgone(maj, mn, mn, VCHR);
132 
133 	return (0);
134 }
135 
136 int
137 ciropen(dev_t dev, int flag, int mode, struct proc *p)
138 {
139 	struct cir_softc *sc;
140 	int error;
141 
142 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
143 	if (sc == NULL)
144 		return (ENXIO);
145 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
146 		return (EIO);
147 	if (sc->sc_open)
148 		return (EBUSY);
149 	if (sc->sc_methods->im_open != NULL) {
150 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p);
151 		if (error)
152 			return (error);
153 	}
154 	sc->sc_open = 1;
155 	return (0);
156 }
157 
158 int
159 circlose(dev_t dev, int flag, int mode, struct proc *p)
160 {
161 	struct cir_softc *sc;
162 	int error;
163 
164 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
165 	if (sc == NULL)
166 		return (ENXIO);
167 	if (sc->sc_methods->im_close != NULL)
168 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p);
169 	else
170 		error = 0;
171 	sc->sc_open = 0;
172 	return (error);
173 }
174 
175 int
176 cirread(dev_t dev, struct uio *uio, int flag)
177 {
178 	struct cir_softc *sc;
179 
180 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
181 	if (sc == NULL)
182 		return (ENXIO);
183 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
184 		return (EIO);
185 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
186 }
187 
188 int
189 cirwrite(dev_t dev, struct uio *uio, int flag)
190 {
191 	struct cir_softc *sc;
192 
193 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
194 	if (sc == NULL)
195 		return (ENXIO);
196 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
197 		return (EIO);
198 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
199 }
200 
201 int
202 cirioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
203 {
204 	struct cir_softc *sc;
205 	int error;
206 
207 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
208 	if (sc == NULL)
209 		return (ENXIO);
210 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
211 		return (EIO);
212 
213 	switch (cmd) {
214 	case FIONBIO:
215 		/* All handled in the upper FS layer. */
216 		error = 0;
217 		break;
218 	case CIR_GET_PARAMS:
219 		*(struct cir_params *)addr = sc->sc_params;
220 		break;
221 	case CIR_SET_PARAMS:
222 		error = sc->sc_methods->im_setparams(sc->sc_handle,
223 			    (struct cir_params *)addr);
224 		if (!error)
225 			sc->sc_params = *(struct cir_params *)addr;
226 		break;
227 	default:
228 		error = EINVAL;
229 		break;
230 	}
231 	return (error);
232 }
233 
234 int
235 cirpoll(dev_t dev, int events, struct proc *p)
236 {
237 	struct cir_softc *sc;
238 	int revents;
239 	int s;
240 
241 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
242 	if (sc == NULL)
243 		return (ENXIO);
244 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
245 		return (EIO);
246 
247 	revents = 0;
248 	s = splir();
249 #if 0
250 	if (events & (POLLIN | POLLRDNORM))
251 		if (sc->sc_rdframes > 0)
252 			revents |= events & (POLLIN | POLLRDNORM);
253 #endif
254 
255 #if 0
256 	/* How about write? */
257 	if (events & (POLLOUT | POLLWRNORM))
258 		if (???)
259 			revents |= events & (POLLOUT | POLLWRNORM);
260 #endif
261 
262 	if (revents == 0) {
263 		if (events & (POLLIN | POLLRDNORM))
264 			selrecord(p, &sc->sc_rdsel);
265 
266 #if 0
267 		if (events & (POLLOUT | POLLWRNORM))
268 			selrecord(p, &sc->sc_wrsel);
269 #endif
270 	}
271 
272 	splx(s);
273 	return (revents);
274 }
275