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