button.c revision 1.9.16.1 1 /* $NetBSD: button.c,v 1.9.16.1 2021/01/03 16:34:54 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.9.16.1 2021/01/03 16:34:54 thorpej Exp $");
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/systm.h>
44 #include <sys/queue.h>
45 #include <sys/mutex.h>
46 #include <sys/errno.h>
47 #include <sys/fcntl.h>
48 #include <sys/callout.h>
49 #include <sys/kernel.h>
50 #include <sys/once.h>
51 #include <sys/poll.h>
52 #include <sys/select.h>
53 #include <sys/vnode.h>
54
55 #include <machine/button.h>
56
57 #include <landisk/dev/buttonvar.h>
58
59 /*
60 * event handler
61 */
62 static ONCE_DECL(btn_once);
63 static LIST_HEAD(, btn_event) btn_event_list;
64 static kmutex_t btn_event_list_lock;
65
66 static struct lwp *btn_daemon;
67
68 #define BTN_MAX_EVENTS 32
69
70 static kmutex_t btn_event_queue_lock;
71 static kcondvar_t btn_event_queue_cv;
72
73 static button_event_t btn_event_queue[BTN_MAX_EVENTS];
74 static int btn_event_queue_head;
75 static int btn_event_queue_tail;
76 static int btn_event_queue_count;
77 static int btn_event_queue_flags;
78 static struct selinfo btn_event_queue_selinfo;
79
80 static char btn_type[32];
81
82 #define BEVQ_F_WAITING 0x01 /* daemon waiting for event */
83
84 #define BTN_NEXT_EVENT(x) (((x) + 1) / BTN_MAX_EVENTS)
85
86 dev_type_open(btnopen);
87 dev_type_close(btnclose);
88 dev_type_ioctl(btnioctl);
89 dev_type_read(btnread);
90 dev_type_poll(btnpoll);
91 dev_type_kqfilter(btnkqfilter);
92
93 const struct cdevsw button_cdevsw = {
94 .d_open = btnopen,
95 .d_close = btnclose,
96 .d_read = btnread,
97 .d_write = nowrite,
98 .d_ioctl = btnioctl,
99 .d_stop = nostop,
100 .d_tty = notty,
101 .d_poll = btnpoll,
102 .d_mmap = nommap,
103 .d_kqfilter = btnkqfilter,
104 .d_discard = nodiscard,
105 .d_flag = 0
106 };
107
108 static int
109 btn_init(void)
110 {
111
112 LIST_INIT(&btn_event_list);
113 mutex_init(&btn_event_list_lock, MUTEX_DEFAULT, IPL_NONE);
114 mutex_init(&btn_event_queue_lock, MUTEX_DEFAULT, IPL_NONE);
115 cv_init(&btn_event_queue_cv, "btncv");
116 selinit(&btn_event_queue_selinfo);
117
118 return 0;
119 }
120
121 static int
122 btn_queue_event(button_event_t *bev)
123 {
124
125 if (btn_event_queue_count == BTN_MAX_EVENTS)
126 return (0);
127
128 btn_event_queue[btn_event_queue_head] = *bev;
129 btn_event_queue_head = BTN_NEXT_EVENT(btn_event_queue_head);
130 btn_event_queue_count++;
131
132 return (1);
133 }
134
135 static int
136 btn_get_event(button_event_t *bev)
137 {
138
139 if (btn_event_queue_count == 0)
140 return (0);
141
142 *bev = btn_event_queue[btn_event_queue_tail];
143 btn_event_queue_tail = BTN_NEXT_EVENT(btn_event_queue_tail);
144 btn_event_queue_count--;
145
146 return (1);
147 }
148
149 static void
150 btn_event_queue_flush(void)
151 {
152
153 btn_event_queue_head = 0;
154 btn_event_queue_tail = 0;
155 btn_event_queue_count = 0;
156 btn_event_queue_flags = 0;
157 }
158
159 int
160 btnopen(dev_t dev, int flag, int mode, struct lwp *l)
161 {
162 int error;
163
164 error = RUN_ONCE(&btn_once, btn_init);
165 if (error) {
166 return error;
167 }
168
169 if (minor(dev) != 0) {
170 return (ENODEV);
171 }
172
173 mutex_enter(&btn_event_queue_lock);
174 if (btn_daemon != NULL) {
175 error = EBUSY;
176 } else {
177 error = 0;
178 btn_daemon = l;
179 btn_event_queue_flush();
180 }
181 mutex_exit(&btn_event_queue_lock);
182
183 return (error);
184 }
185
186 int
187 btnclose(dev_t dev, int flag, int mode, struct lwp *l)
188 {
189 int count;
190
191 if (minor(dev) != 0) {
192 return (ENODEV);
193 }
194
195 mutex_enter(&btn_event_queue_lock);
196 count = btn_event_queue_count;
197 btn_daemon = NULL;
198 btn_event_queue_flush();
199 mutex_exit(&btn_event_queue_lock);
200
201 if (count) {
202 printf("WARNING: %d events lost by exiting daemon\n", count);
203 }
204
205 return (0);
206 }
207
208 int
209 btnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
210 {
211 int error = 0;
212
213 if (minor(dev) != 0) {
214 return (ENODEV);
215 }
216
217 switch (cmd) {
218 case BUTTON_IOC_GET_TYPE:
219 {
220 struct button_type *button_type = (void *)data;
221 strcpy(button_type->button_type, btn_type);
222 break;
223 }
224
225 default:
226 error = ENOTTY;
227 break;
228 }
229
230 return (error);
231 }
232
233 int
234 btnread(dev_t dev, struct uio *uio, int flags)
235 {
236 button_event_t bev;
237 int error;
238
239 if (minor(dev) != 0) {
240 return (ENODEV);
241 }
242
243 if (uio->uio_resid != BUTTON_EVENT_MSG_SIZE) {
244 return (EINVAL);
245 }
246
247 mutex_enter(&btn_event_queue_lock);
248 for (;;) {
249 if (btn_get_event(&bev)) {
250 mutex_exit(&btn_event_queue_lock);
251 return (uiomove(&bev, BUTTON_EVENT_MSG_SIZE, uio));
252 }
253
254 if (flags & IO_NDELAY) {
255 mutex_exit(&btn_event_queue_lock);
256 return (EWOULDBLOCK);
257 }
258
259 btn_event_queue_flags |= BEVQ_F_WAITING;
260 error = cv_wait_sig(&btn_event_queue_cv, &btn_event_queue_lock);
261 if (error) {
262 mutex_exit(&btn_event_queue_lock);
263 return (error);
264 }
265 }
266 }
267
268 int
269 btnpoll(dev_t dev, int events, struct lwp *l)
270 {
271 int revents;
272
273 if (minor(dev) != 0) {
274 return (ENODEV);
275 }
276
277 revents = events & (POLLOUT | POLLWRNORM);
278
279 /* Attempt to save some work. */
280 if ((events & (POLLIN | POLLRDNORM)) == 0)
281 return (revents);
282
283 mutex_enter(&btn_event_queue_lock);
284 if (btn_event_queue_count) {
285 revents |= events & (POLLIN | POLLRDNORM);
286 } else {
287 selrecord(l, &btn_event_queue_selinfo);
288 }
289 mutex_exit(&btn_event_queue_lock);
290
291 return (revents);
292 }
293
294 static void
295 filt_btn_rdetach(struct knote *kn)
296 {
297
298 mutex_enter(&btn_event_queue_lock);
299 selremove_knote(&btn_event_queue_selinfo, kn);
300 mutex_exit(&btn_event_queue_lock);
301 }
302
303 static int
304 filt_btn_read(struct knote *kn, long hint)
305 {
306
307 mutex_enter(&btn_event_queue_lock);
308 kn->kn_data = btn_event_queue_count;
309 mutex_exit(&btn_event_queue_lock);
310
311 return (kn->kn_data > 0);
312 }
313
314 static const struct filterops btn_read_filtops = {
315 .f_isfd = 1,
316 .f_attach = NULL,
317 .f_detach = filt_btn_rdetach,
318 .f_event = filt_btn_read,
319 };
320
321 static const struct filterops btn_write_filtops = {
322 .f_isfd = 1,
323 .f_attach = NULL,
324 .f_detach = filt_btn_rdetach,
325 .f_event = filt_seltrue,
326 };
327
328 int
329 btnkqfilter(dev_t dev, struct knote *kn)
330 {
331
332 if (minor(dev) != 0) {
333 return (ENODEV);
334 }
335
336 switch (kn->kn_filter) {
337 case EVFILT_READ:
338 kn->kn_fop = &btn_read_filtops;
339 break;
340
341 case EVFILT_WRITE:
342 kn->kn_fop = &btn_write_filtops;
343 break;
344
345 default:
346 return (1);
347 }
348
349 mutex_enter(&btn_event_queue_lock);
350 selrecord_knote(&btn_event_queue_selinfo, kn);
351 mutex_exit(&btn_event_queue_lock);
352
353 return (0);
354 }
355
356 void
357 btn_settype(const char *type)
358 {
359
360 /*
361 * Don't bother locking this; it's going to be set
362 * during autoconfiguration, and then only read from
363 * then on.
364 */
365 strlcpy(btn_type, type, sizeof(btn_type));
366 }
367
368 int
369 btn_event_register(struct btn_event *bev)
370 {
371
372 mutex_enter(&btn_event_list_lock);
373 LIST_INSERT_HEAD(&btn_event_list, bev, bev_list);
374 mutex_exit(&btn_event_list_lock);
375
376 return (0);
377 }
378
379 void
380 btn_event_unregister(struct btn_event *bev)
381 {
382
383 mutex_enter(&btn_event_list_lock);
384 LIST_REMOVE(bev, bev_list);
385 mutex_exit(&btn_event_list_lock);
386 }
387
388 void
389 btn_event_send(struct btn_event *bev, int event)
390 {
391 button_event_t btnev;
392 int rv;
393
394 mutex_enter(&btn_event_queue_lock);
395 if (btn_daemon == NULL) {
396 mutex_exit(&btn_event_queue_lock);
397 printf("%s: btn_event_send can't handle me.\n", bev->bev_name);
398 return;
399 }
400
401 btnev.bev_type = BUTTON_EVENT_STATE_CHANGE;
402 btnev.bev_event.bs_state = event;
403 strcpy(btnev.bev_event.bs_name, bev->bev_name);
404
405 rv = btn_queue_event(&btnev);
406 if (rv == 0) {
407 mutex_exit(&btn_event_queue_lock);
408 printf("%s: WARNING: state change event %d lost; "
409 "queue full\n", bev->bev_name, btnev.bev_type);
410 return;
411 }
412 if (btn_event_queue_flags & BEVQ_F_WAITING) {
413 btn_event_queue_flags &= ~BEVQ_F_WAITING;
414 cv_broadcast(&btn_event_queue_cv);
415 }
416 selnotify(&btn_event_queue_selinfo, 0, 0);
417 mutex_exit(&btn_event_queue_lock);
418 }
419