button.c revision 1.7 1 1.7 dholland /* $NetBSD: button.c,v 1.7 2014/03/16 05:20:24 dholland 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.7 dholland __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.7 2014/03/16 05:20:24 dholland 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 ONCE_DECL(btn_once);
63 1.6 rmind static LIST_HEAD(, btn_event) btn_event_list;
64 1.6 rmind static kmutex_t btn_event_list_lock;
65 1.1 uwe
66 1.1 uwe static struct lwp *btn_daemon;
67 1.1 uwe
68 1.1 uwe #define BTN_MAX_EVENTS 32
69 1.1 uwe
70 1.6 rmind static kmutex_t btn_event_queue_lock;
71 1.6 rmind static kcondvar_t btn_event_queue_cv;
72 1.6 rmind
73 1.1 uwe static button_event_t btn_event_queue[BTN_MAX_EVENTS];
74 1.1 uwe static int btn_event_queue_head;
75 1.1 uwe static int btn_event_queue_tail;
76 1.1 uwe static int btn_event_queue_count;
77 1.1 uwe static int btn_event_queue_flags;
78 1.1 uwe static struct selinfo btn_event_queue_selinfo;
79 1.1 uwe
80 1.1 uwe static char btn_type[32];
81 1.1 uwe
82 1.1 uwe #define BEVQ_F_WAITING 0x01 /* daemon waiting for event */
83 1.1 uwe
84 1.1 uwe #define BTN_NEXT_EVENT(x) (((x) + 1) / BTN_MAX_EVENTS)
85 1.1 uwe
86 1.1 uwe dev_type_open(btnopen);
87 1.1 uwe dev_type_close(btnclose);
88 1.1 uwe dev_type_ioctl(btnioctl);
89 1.1 uwe dev_type_read(btnread);
90 1.1 uwe dev_type_poll(btnpoll);
91 1.1 uwe dev_type_kqfilter(btnkqfilter);
92 1.1 uwe
93 1.1 uwe const struct cdevsw button_cdevsw = {
94 1.7 dholland .d_open = btnopen,
95 1.7 dholland .d_close = btnclose,
96 1.7 dholland .d_read = btnread,
97 1.7 dholland .d_write = nowrite,
98 1.7 dholland .d_ioctl = btnioctl,
99 1.7 dholland .d_stop = nostop,
100 1.7 dholland .d_tty = notty,
101 1.7 dholland .d_poll = btnpoll,
102 1.7 dholland .d_mmap = nommap,
103 1.7 dholland .d_kqfilter = btnkqfilter,
104 1.7 dholland .d_flag = 0
105 1.1 uwe };
106 1.1 uwe
107 1.1 uwe static 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.6 rmind error = RUN_ONCE(&btn_once, btn_init);
164 1.6 rmind if (error) {
165 1.6 rmind return error;
166 1.5 rmind }
167 1.5 rmind
168 1.1 uwe if (minor(dev) != 0) {
169 1.1 uwe return (ENODEV);
170 1.1 uwe }
171 1.1 uwe
172 1.6 rmind mutex_enter(&btn_event_queue_lock);
173 1.1 uwe if (btn_daemon != NULL) {
174 1.1 uwe error = EBUSY;
175 1.1 uwe } else {
176 1.1 uwe error = 0;
177 1.1 uwe btn_daemon = l;
178 1.1 uwe btn_event_queue_flush();
179 1.1 uwe }
180 1.6 rmind mutex_exit(&btn_event_queue_lock);
181 1.1 uwe
182 1.1 uwe return (error);
183 1.1 uwe }
184 1.1 uwe
185 1.1 uwe int
186 1.1 uwe btnclose(dev_t dev, int flag, int mode, struct lwp *l)
187 1.1 uwe {
188 1.1 uwe int count;
189 1.1 uwe
190 1.1 uwe if (minor(dev) != 0) {
191 1.1 uwe return (ENODEV);
192 1.1 uwe }
193 1.1 uwe
194 1.6 rmind mutex_enter(&btn_event_queue_lock);
195 1.1 uwe count = btn_event_queue_count;
196 1.1 uwe btn_daemon = NULL;
197 1.1 uwe btn_event_queue_flush();
198 1.6 rmind mutex_exit(&btn_event_queue_lock);
199 1.1 uwe
200 1.1 uwe if (count) {
201 1.1 uwe printf("WARNING: %d events lost by exiting daemon\n", count);
202 1.1 uwe }
203 1.1 uwe
204 1.1 uwe return (0);
205 1.1 uwe }
206 1.1 uwe
207 1.1 uwe int
208 1.2 christos btnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
209 1.1 uwe {
210 1.1 uwe int error = 0;
211 1.1 uwe
212 1.1 uwe if (minor(dev) != 0) {
213 1.1 uwe return (ENODEV);
214 1.1 uwe }
215 1.1 uwe
216 1.1 uwe switch (cmd) {
217 1.1 uwe case BUTTON_IOC_GET_TYPE:
218 1.1 uwe {
219 1.1 uwe struct button_type *button_type = (void *)data;
220 1.1 uwe strcpy(button_type->button_type, btn_type);
221 1.1 uwe break;
222 1.1 uwe }
223 1.1 uwe
224 1.1 uwe default:
225 1.1 uwe error = ENOTTY;
226 1.1 uwe break;
227 1.1 uwe }
228 1.1 uwe
229 1.1 uwe return (error);
230 1.1 uwe }
231 1.1 uwe
232 1.1 uwe int
233 1.1 uwe btnread(dev_t dev, struct uio *uio, int flags)
234 1.1 uwe {
235 1.1 uwe button_event_t bev;
236 1.1 uwe int error;
237 1.1 uwe
238 1.1 uwe if (minor(dev) != 0) {
239 1.1 uwe return (ENODEV);
240 1.1 uwe }
241 1.1 uwe
242 1.1 uwe if (uio->uio_resid != BUTTON_EVENT_MSG_SIZE) {
243 1.1 uwe return (EINVAL);
244 1.1 uwe }
245 1.1 uwe
246 1.6 rmind mutex_enter(&btn_event_queue_lock);
247 1.1 uwe for (;;) {
248 1.1 uwe if (btn_get_event(&bev)) {
249 1.6 rmind mutex_exit(&btn_event_queue_lock);
250 1.1 uwe return (uiomove(&bev, BUTTON_EVENT_MSG_SIZE, uio));
251 1.1 uwe }
252 1.1 uwe
253 1.1 uwe if (flags & IO_NDELAY) {
254 1.6 rmind mutex_exit(&btn_event_queue_lock);
255 1.1 uwe return (EWOULDBLOCK);
256 1.1 uwe }
257 1.1 uwe
258 1.1 uwe btn_event_queue_flags |= BEVQ_F_WAITING;
259 1.6 rmind error = cv_wait_sig(&btn_event_queue_cv, &btn_event_queue_lock);
260 1.1 uwe if (error) {
261 1.6 rmind mutex_exit(&btn_event_queue_lock);
262 1.1 uwe return (error);
263 1.1 uwe }
264 1.1 uwe }
265 1.1 uwe }
266 1.1 uwe
267 1.1 uwe int
268 1.1 uwe btnpoll(dev_t dev, int events, struct lwp *l)
269 1.1 uwe {
270 1.1 uwe int revents;
271 1.1 uwe
272 1.1 uwe if (minor(dev) != 0) {
273 1.1 uwe return (ENODEV);
274 1.1 uwe }
275 1.1 uwe
276 1.1 uwe revents = events & (POLLOUT | POLLWRNORM);
277 1.1 uwe
278 1.1 uwe /* Attempt to save some work. */
279 1.1 uwe if ((events & (POLLIN | POLLRDNORM)) == 0)
280 1.1 uwe return (revents);
281 1.1 uwe
282 1.6 rmind mutex_enter(&btn_event_queue_lock);
283 1.1 uwe if (btn_event_queue_count) {
284 1.1 uwe revents |= events & (POLLIN | POLLRDNORM);
285 1.1 uwe } else {
286 1.1 uwe selrecord(l, &btn_event_queue_selinfo);
287 1.1 uwe }
288 1.6 rmind mutex_exit(&btn_event_queue_lock);
289 1.1 uwe
290 1.1 uwe return (revents);
291 1.1 uwe }
292 1.1 uwe
293 1.1 uwe static void
294 1.1 uwe filt_btn_rdetach(struct knote *kn)
295 1.1 uwe {
296 1.1 uwe
297 1.6 rmind mutex_enter(&btn_event_queue_lock);
298 1.1 uwe SLIST_REMOVE(&btn_event_queue_selinfo.sel_klist,
299 1.1 uwe kn, knote, kn_selnext);
300 1.6 rmind mutex_exit(&btn_event_queue_lock);
301 1.1 uwe }
302 1.1 uwe
303 1.1 uwe static int
304 1.1 uwe filt_btn_read(struct knote *kn, long hint)
305 1.1 uwe {
306 1.1 uwe
307 1.6 rmind mutex_enter(&btn_event_queue_lock);
308 1.1 uwe kn->kn_data = btn_event_queue_count;
309 1.6 rmind mutex_exit(&btn_event_queue_lock);
310 1.1 uwe
311 1.1 uwe return (kn->kn_data > 0);
312 1.1 uwe }
313 1.1 uwe
314 1.1 uwe static const struct filterops btn_read_filtops =
315 1.1 uwe { 1, NULL, filt_btn_rdetach, filt_btn_read };
316 1.1 uwe
317 1.1 uwe static const struct filterops btn_write_filtops =
318 1.1 uwe { 1, NULL, filt_btn_rdetach, filt_seltrue };
319 1.1 uwe
320 1.1 uwe int
321 1.1 uwe btnkqfilter(dev_t dev, struct knote *kn)
322 1.1 uwe {
323 1.1 uwe struct klist *klist;
324 1.1 uwe
325 1.1 uwe if (minor(dev) != 0) {
326 1.1 uwe return (ENODEV);
327 1.1 uwe }
328 1.1 uwe
329 1.1 uwe switch (kn->kn_filter) {
330 1.1 uwe case EVFILT_READ:
331 1.1 uwe klist = &btn_event_queue_selinfo.sel_klist;
332 1.1 uwe kn->kn_fop = &btn_read_filtops;
333 1.1 uwe break;
334 1.1 uwe
335 1.1 uwe case EVFILT_WRITE:
336 1.1 uwe klist = &btn_event_queue_selinfo.sel_klist;
337 1.1 uwe kn->kn_fop = &btn_write_filtops;
338 1.1 uwe break;
339 1.1 uwe
340 1.1 uwe default:
341 1.1 uwe return (1);
342 1.1 uwe }
343 1.1 uwe
344 1.6 rmind mutex_enter(&btn_event_queue_lock);
345 1.1 uwe SLIST_INSERT_HEAD(klist, kn, kn_selnext);
346 1.6 rmind mutex_exit(&btn_event_queue_lock);
347 1.1 uwe
348 1.1 uwe return (0);
349 1.1 uwe }
350 1.1 uwe
351 1.1 uwe void
352 1.1 uwe btn_settype(const char *type)
353 1.1 uwe {
354 1.1 uwe
355 1.1 uwe /*
356 1.1 uwe * Don't bother locking this; it's going to be set
357 1.1 uwe * during autoconfiguration, and then only read from
358 1.1 uwe * then on.
359 1.1 uwe */
360 1.1 uwe strlcpy(btn_type, type, sizeof(btn_type));
361 1.1 uwe }
362 1.1 uwe
363 1.1 uwe int
364 1.1 uwe btn_event_register(struct btn_event *bev)
365 1.1 uwe {
366 1.1 uwe
367 1.6 rmind mutex_enter(&btn_event_list_lock);
368 1.1 uwe LIST_INSERT_HEAD(&btn_event_list, bev, bev_list);
369 1.6 rmind mutex_exit(&btn_event_list_lock);
370 1.1 uwe
371 1.1 uwe return (0);
372 1.1 uwe }
373 1.1 uwe
374 1.1 uwe void
375 1.1 uwe btn_event_unregister(struct btn_event *bev)
376 1.1 uwe {
377 1.1 uwe
378 1.6 rmind mutex_enter(&btn_event_list_lock);
379 1.1 uwe LIST_REMOVE(bev, bev_list);
380 1.6 rmind mutex_exit(&btn_event_list_lock);
381 1.1 uwe }
382 1.1 uwe
383 1.1 uwe void
384 1.1 uwe btn_event_send(struct btn_event *bev, int event)
385 1.1 uwe {
386 1.1 uwe button_event_t btnev;
387 1.1 uwe int rv;
388 1.1 uwe
389 1.6 rmind mutex_enter(&btn_event_queue_lock);
390 1.6 rmind if (btn_daemon == NULL) {
391 1.6 rmind mutex_exit(&btn_event_queue_lock);
392 1.6 rmind printf("%s: btn_event_send can't handle me.\n", bev->bev_name);
393 1.1 uwe return;
394 1.1 uwe }
395 1.1 uwe
396 1.6 rmind btnev.bev_type = BUTTON_EVENT_STATE_CHANGE;
397 1.6 rmind btnev.bev_event.bs_state = event;
398 1.6 rmind strcpy(btnev.bev_event.bs_name, bev->bev_name);
399 1.6 rmind
400 1.6 rmind rv = btn_queue_event(&btnev);
401 1.6 rmind if (rv == 0) {
402 1.6 rmind mutex_exit(&btn_event_queue_lock);
403 1.6 rmind printf("%s: WARNING: state change event %d lost; "
404 1.6 rmind "queue full\n", bev->bev_name, btnev.bev_type);
405 1.6 rmind return;
406 1.6 rmind }
407 1.6 rmind if (btn_event_queue_flags & BEVQ_F_WAITING) {
408 1.6 rmind btn_event_queue_flags &= ~BEVQ_F_WAITING;
409 1.6 rmind cv_broadcast(&btn_event_queue_cv);
410 1.6 rmind }
411 1.6 rmind selnotify(&btn_event_queue_selinfo, 0, 0);
412 1.6 rmind mutex_exit(&btn_event_queue_lock);
413 1.1 uwe }
414