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