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