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