cir.c revision 1.25 1 /* $NetBSD: cir.c,v 1.25 2009/05/12 14:29:42 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 *
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.25 2009/05/12 14:29:42 cegger 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
45 #include <dev/ir/ir.h>
46 #include <dev/ir/cirio.h>
47 #include <dev/ir/cirvar.h>
48
49 dev_type_open(ciropen);
50 dev_type_close(circlose);
51 dev_type_read(cirread);
52 dev_type_write(cirwrite);
53 dev_type_ioctl(cirioctl);
54 dev_type_poll(cirpoll);
55
56 const struct cdevsw cir_cdevsw = {
57 ciropen, circlose, cirread, cirwrite, cirioctl,
58 nostop, notty, cirpoll, nommap, nokqfilter,
59 D_OTHER
60 };
61
62 int cir_match(device_t parent, cfdata_t match, void *aux);
63 void cir_attach(device_t parent, device_t self, void *aux);
64 int cir_activate(device_t self, enum devact act);
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, cir_activate);
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_activate(device_t self, enum devact act)
103 {
104 /*struct cir_softc *sc = device_private(self);*/
105
106 switch (act) {
107 case DVACT_ACTIVATE:
108 return (EOPNOTSUPP);
109 break;
110
111 case DVACT_DEACTIVATE:
112 break;
113 }
114 return (0);
115 }
116
117 int
118 cir_detach(device_t self, int flags)
119 {
120 struct cir_softc *sc = device_private(self);
121 int maj, mn;
122
123 /* locate the major number */
124 maj = cdevsw_lookup_major(&cir_cdevsw);
125
126 /* Nuke the vnodes for any open instances (calls close). */
127 mn = device_unit(self);
128 vdevgone(maj, mn, mn, VCHR);
129
130 seldestroy(&sc->sc_rdsel);
131
132 return (0);
133 }
134
135 int
136 ciropen(dev_t dev, int flag, int mode, struct lwp *l)
137 {
138 struct cir_softc *sc;
139 int error;
140
141 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
142 if (sc == NULL)
143 return (ENXIO);
144 if (!device_is_active(&sc->sc_dev))
145 return (EIO);
146 if (sc->sc_open)
147 return (EBUSY);
148
149 sc->sc_rdframes = 0;
150 if (sc->sc_methods->im_open != NULL) {
151 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
152 l->l_proc);
153 if (error)
154 return (error);
155 }
156 sc->sc_open = 1;
157 return (0);
158 }
159
160 int
161 circlose(dev_t dev, int flag, int mode, struct lwp *l)
162 {
163 struct cir_softc *sc;
164 int error;
165
166 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
167 if (sc == NULL)
168 return (ENXIO);
169 if (sc->sc_methods->im_close != NULL)
170 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
171 l->l_proc);
172 else
173 error = 0;
174 sc->sc_open = 0;
175 return (error);
176 }
177
178 int
179 cirread(dev_t dev, struct uio *uio, int flag)
180 {
181 struct cir_softc *sc;
182
183 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
184 if (sc == NULL)
185 return (ENXIO);
186 if (!device_is_active(&sc->sc_dev))
187 return (EIO);
188 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
189 }
190
191 int
192 cirwrite(dev_t dev, struct uio *uio, int flag)
193 {
194 struct cir_softc *sc;
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 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
202 }
203
204 int
205 cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
206 {
207 struct cir_softc *sc;
208 int error;
209
210 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
211 if (sc == NULL)
212 return (ENXIO);
213 if (!device_is_active(&sc->sc_dev))
214 return (EIO);
215
216 switch (cmd) {
217 case FIONBIO:
218 /* All handled in the upper FS layer. */
219 error = 0;
220 break;
221 case CIR_GET_PARAMS:
222 *(struct cir_params *)addr = sc->sc_params;
223 error = 0;
224 break;
225 case CIR_SET_PARAMS:
226 error = sc->sc_methods->im_setparams(sc->sc_handle,
227 (struct cir_params *)addr);
228 if (!error)
229 sc->sc_params = *(struct cir_params *)addr;
230 break;
231 default:
232 error = EINVAL;
233 break;
234 }
235 return (error);
236 }
237
238 int
239 cirpoll(dev_t dev, int events, struct lwp *l)
240 {
241 struct cir_softc *sc;
242 int revents;
243 int s;
244
245 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
246 if (sc == NULL)
247 return (POLLERR);
248 if (!device_is_active(&sc->sc_dev))
249 return (POLLERR);
250
251 revents = 0;
252 s = splir();
253 if (events & (POLLIN | POLLRDNORM))
254 if (sc->sc_rdframes > 0)
255 revents |= events & (POLLIN | POLLRDNORM);
256
257 #if 0
258 /* How about write? */
259 if (events & (POLLOUT | POLLWRNORM))
260 if (/* ??? */)
261 revents |= events & (POLLOUT | POLLWRNORM);
262 #endif
263
264 if (revents == 0) {
265 if (events & (POLLIN | POLLRDNORM))
266 selrecord(l, &sc->sc_rdsel);
267
268 #if 0
269 if (events & (POLLOUT | POLLWRNORM))
270 selrecord(p, &sc->sc_wrsel);
271 #endif
272 }
273
274 splx(s);
275 return (revents);
276 }
277