cir.c revision 1.26 1 1.26 dyoung /* $NetBSD: cir.c,v 1.26 2009/12/06 22:40:56 dyoung Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
30 1.1 augustss */
31 1.9 lukem
32 1.9 lukem #include <sys/cdefs.h>
33 1.26 dyoung __KERNEL_RCSID(0, "$NetBSD: cir.c,v 1.26 2009/12/06 22:40:56 dyoung Exp $");
34 1.1 augustss
35 1.1 augustss #include <sys/param.h>
36 1.1 augustss #include <sys/systm.h>
37 1.1 augustss #include <sys/ioctl.h>
38 1.1 augustss #include <sys/kernel.h>
39 1.1 augustss #include <sys/device.h>
40 1.1 augustss #include <sys/conf.h>
41 1.1 augustss #include <sys/poll.h>
42 1.1 augustss #include <sys/select.h>
43 1.1 augustss #include <sys/vnode.h>
44 1.1 augustss
45 1.2 augustss #include <dev/ir/ir.h>
46 1.2 augustss #include <dev/ir/cirio.h>
47 1.1 augustss #include <dev/ir/cirvar.h>
48 1.1 augustss
49 1.3 gehenna dev_type_open(ciropen);
50 1.3 gehenna dev_type_close(circlose);
51 1.3 gehenna dev_type_read(cirread);
52 1.3 gehenna dev_type_write(cirwrite);
53 1.3 gehenna dev_type_ioctl(cirioctl);
54 1.3 gehenna dev_type_poll(cirpoll);
55 1.3 gehenna
56 1.3 gehenna const struct cdevsw cir_cdevsw = {
57 1.3 gehenna ciropen, circlose, cirread, cirwrite, cirioctl,
58 1.22 jmcneill nostop, notty, cirpoll, nommap, nokqfilter,
59 1.16 cube D_OTHER
60 1.3 gehenna };
61 1.1 augustss
62 1.25 cegger int cir_match(device_t parent, cfdata_t match, void *aux);
63 1.25 cegger void cir_attach(device_t parent, device_t self, void *aux);
64 1.25 cegger int cir_detach(device_t self, int flags);
65 1.1 augustss
66 1.6 thorpej CFATTACH_DECL(cir, sizeof(struct cir_softc),
67 1.26 dyoung cir_match, cir_attach, cir_detach, NULL);
68 1.1 augustss
69 1.1 augustss extern struct cfdriver cir_cd;
70 1.1 augustss
71 1.1 augustss #define CIRUNIT(dev) (minor(dev))
72 1.1 augustss
73 1.1 augustss int
74 1.25 cegger cir_match(device_t parent, cfdata_t match, void *aux)
75 1.1 augustss {
76 1.1 augustss struct ir_attach_args *ia = aux;
77 1.1 augustss
78 1.1 augustss return (ia->ia_type == IR_TYPE_CIR);
79 1.1 augustss }
80 1.1 augustss
81 1.1 augustss void
82 1.25 cegger cir_attach(device_t parent, device_t self, void *aux)
83 1.1 augustss {
84 1.14 thorpej struct cir_softc *sc = device_private(self);
85 1.1 augustss struct ir_attach_args *ia = aux;
86 1.1 augustss
87 1.18 rmind selinit(&sc->sc_rdsel);
88 1.1 augustss sc->sc_methods = ia->ia_methods;
89 1.1 augustss sc->sc_handle = ia->ia_handle;
90 1.1 augustss
91 1.1 augustss #ifdef DIAGNOSTIC
92 1.1 augustss if (sc->sc_methods->im_read == NULL ||
93 1.2 augustss sc->sc_methods->im_write == NULL ||
94 1.2 augustss sc->sc_methods->im_setparams == NULL)
95 1.19 cegger panic("%s: missing methods", device_xname(&sc->sc_dev));
96 1.1 augustss #endif
97 1.1 augustss printf("\n");
98 1.1 augustss }
99 1.1 augustss
100 1.1 augustss int
101 1.25 cegger cir_detach(device_t self, int flags)
102 1.1 augustss {
103 1.18 rmind struct cir_softc *sc = device_private(self);
104 1.1 augustss int maj, mn;
105 1.1 augustss
106 1.1 augustss /* locate the major number */
107 1.3 gehenna maj = cdevsw_lookup_major(&cir_cdevsw);
108 1.1 augustss
109 1.1 augustss /* Nuke the vnodes for any open instances (calls close). */
110 1.13 thorpej mn = device_unit(self);
111 1.1 augustss vdevgone(maj, mn, mn, VCHR);
112 1.1 augustss
113 1.18 rmind seldestroy(&sc->sc_rdsel);
114 1.18 rmind
115 1.1 augustss return (0);
116 1.1 augustss }
117 1.1 augustss
118 1.1 augustss int
119 1.16 cube ciropen(dev_t dev, int flag, int mode, struct lwp *l)
120 1.1 augustss {
121 1.1 augustss struct cir_softc *sc;
122 1.1 augustss int error;
123 1.1 augustss
124 1.21 cegger sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
125 1.1 augustss if (sc == NULL)
126 1.1 augustss return (ENXIO);
127 1.12 thorpej if (!device_is_active(&sc->sc_dev))
128 1.1 augustss return (EIO);
129 1.1 augustss if (sc->sc_open)
130 1.1 augustss return (EBUSY);
131 1.23 jmcneill
132 1.23 jmcneill sc->sc_rdframes = 0;
133 1.1 augustss if (sc->sc_methods->im_open != NULL) {
134 1.16 cube error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
135 1.16 cube l->l_proc);
136 1.1 augustss if (error)
137 1.1 augustss return (error);
138 1.1 augustss }
139 1.1 augustss sc->sc_open = 1;
140 1.1 augustss return (0);
141 1.1 augustss }
142 1.1 augustss
143 1.1 augustss int
144 1.16 cube circlose(dev_t dev, int flag, int mode, struct lwp *l)
145 1.1 augustss {
146 1.1 augustss struct cir_softc *sc;
147 1.1 augustss int error;
148 1.1 augustss
149 1.21 cegger sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
150 1.1 augustss if (sc == NULL)
151 1.1 augustss return (ENXIO);
152 1.1 augustss if (sc->sc_methods->im_close != NULL)
153 1.16 cube error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
154 1.16 cube l->l_proc);
155 1.1 augustss else
156 1.1 augustss error = 0;
157 1.1 augustss sc->sc_open = 0;
158 1.1 augustss return (error);
159 1.1 augustss }
160 1.1 augustss
161 1.1 augustss int
162 1.1 augustss cirread(dev_t dev, struct uio *uio, int flag)
163 1.1 augustss {
164 1.1 augustss struct cir_softc *sc;
165 1.1 augustss
166 1.21 cegger sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
167 1.1 augustss if (sc == NULL)
168 1.1 augustss return (ENXIO);
169 1.12 thorpej if (!device_is_active(&sc->sc_dev))
170 1.1 augustss return (EIO);
171 1.1 augustss return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
172 1.1 augustss }
173 1.1 augustss
174 1.1 augustss int
175 1.1 augustss cirwrite(dev_t dev, struct uio *uio, int flag)
176 1.1 augustss {
177 1.1 augustss struct cir_softc *sc;
178 1.1 augustss
179 1.21 cegger sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
180 1.1 augustss if (sc == NULL)
181 1.1 augustss return (ENXIO);
182 1.12 thorpej if (!device_is_active(&sc->sc_dev))
183 1.1 augustss return (EIO);
184 1.1 augustss return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
185 1.1 augustss }
186 1.1 augustss
187 1.1 augustss int
188 1.17 christos cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
189 1.1 augustss {
190 1.1 augustss struct cir_softc *sc;
191 1.1 augustss int error;
192 1.1 augustss
193 1.21 cegger sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
194 1.1 augustss if (sc == NULL)
195 1.1 augustss return (ENXIO);
196 1.12 thorpej if (!device_is_active(&sc->sc_dev))
197 1.1 augustss return (EIO);
198 1.1 augustss
199 1.1 augustss switch (cmd) {
200 1.1 augustss case FIONBIO:
201 1.1 augustss /* All handled in the upper FS layer. */
202 1.1 augustss error = 0;
203 1.2 augustss break;
204 1.2 augustss case CIR_GET_PARAMS:
205 1.2 augustss *(struct cir_params *)addr = sc->sc_params;
206 1.16 cube error = 0;
207 1.2 augustss break;
208 1.2 augustss case CIR_SET_PARAMS:
209 1.2 augustss error = sc->sc_methods->im_setparams(sc->sc_handle,
210 1.2 augustss (struct cir_params *)addr);
211 1.2 augustss if (!error)
212 1.2 augustss sc->sc_params = *(struct cir_params *)addr;
213 1.1 augustss break;
214 1.1 augustss default:
215 1.1 augustss error = EINVAL;
216 1.1 augustss break;
217 1.1 augustss }
218 1.1 augustss return (error);
219 1.1 augustss }
220 1.1 augustss
221 1.1 augustss int
222 1.16 cube cirpoll(dev_t dev, int events, struct lwp *l)
223 1.1 augustss {
224 1.1 augustss struct cir_softc *sc;
225 1.1 augustss int revents;
226 1.1 augustss int s;
227 1.1 augustss
228 1.21 cegger sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
229 1.1 augustss if (sc == NULL)
230 1.11 augustss return (POLLERR);
231 1.12 thorpej if (!device_is_active(&sc->sc_dev))
232 1.11 augustss return (POLLERR);
233 1.1 augustss
234 1.1 augustss revents = 0;
235 1.1 augustss s = splir();
236 1.1 augustss if (events & (POLLIN | POLLRDNORM))
237 1.1 augustss if (sc->sc_rdframes > 0)
238 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
239 1.1 augustss
240 1.1 augustss #if 0
241 1.1 augustss /* How about write? */
242 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
243 1.15 cube if (/* ??? */)
244 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
245 1.1 augustss #endif
246 1.1 augustss
247 1.1 augustss if (revents == 0) {
248 1.1 augustss if (events & (POLLIN | POLLRDNORM))
249 1.16 cube selrecord(l, &sc->sc_rdsel);
250 1.1 augustss
251 1.1 augustss #if 0
252 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
253 1.1 augustss selrecord(p, &sc->sc_wrsel);
254 1.1 augustss #endif
255 1.1 augustss }
256 1.1 augustss
257 1.1 augustss splx(s);
258 1.1 augustss return (revents);
259 1.1 augustss }
260