button.c revision 1.6 1 1.6 rmind /* $NetBSD: button.c,v 1.6 2011/05/14 02:27:35 rmind 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.6 rmind __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.6 2011/05/14 02:27:35 rmind 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.1 uwe btnopen, btnclose, btnread, nowrite, btnioctl,
95 1.1 uwe nostop, notty, btnpoll, nommap, btnkqfilter,
96 1.1 uwe };
97 1.1 uwe
98 1.1 uwe static int
99 1.6 rmind btn_init(void)
100 1.6 rmind {
101 1.6 rmind
102 1.6 rmind LIST_INIT(&btn_event_list);
103 1.6 rmind mutex_init(&btn_event_list_lock, MUTEX_DEFAULT, IPL_NONE);
104 1.6 rmind mutex_init(&btn_event_queue_lock, MUTEX_DEFAULT, IPL_NONE);
105 1.6 rmind cv_init(&btn_event_queue_cv, "btncv");
106 1.6 rmind selinit(&btn_event_queue_selinfo);
107 1.6 rmind
108 1.6 rmind return 0;
109 1.6 rmind }
110 1.6 rmind
111 1.6 rmind static int
112 1.1 uwe btn_queue_event(button_event_t *bev)
113 1.1 uwe {
114 1.1 uwe
115 1.1 uwe if (btn_event_queue_count == BTN_MAX_EVENTS)
116 1.1 uwe return (0);
117 1.1 uwe
118 1.1 uwe btn_event_queue[btn_event_queue_head] = *bev;
119 1.1 uwe btn_event_queue_head = BTN_NEXT_EVENT(btn_event_queue_head);
120 1.1 uwe btn_event_queue_count++;
121 1.1 uwe
122 1.1 uwe return (1);
123 1.1 uwe }
124 1.1 uwe
125 1.1 uwe static int
126 1.1 uwe btn_get_event(button_event_t *bev)
127 1.1 uwe {
128 1.1 uwe
129 1.1 uwe if (btn_event_queue_count == 0)
130 1.1 uwe return (0);
131 1.1 uwe
132 1.1 uwe *bev = btn_event_queue[btn_event_queue_tail];
133 1.1 uwe btn_event_queue_tail = BTN_NEXT_EVENT(btn_event_queue_tail);
134 1.1 uwe btn_event_queue_count--;
135 1.1 uwe
136 1.1 uwe return (1);
137 1.1 uwe }
138 1.1 uwe
139 1.1 uwe static void
140 1.1 uwe btn_event_queue_flush(void)
141 1.1 uwe {
142 1.1 uwe
143 1.1 uwe btn_event_queue_head = 0;
144 1.1 uwe btn_event_queue_tail = 0;
145 1.1 uwe btn_event_queue_count = 0;
146 1.1 uwe btn_event_queue_flags = 0;
147 1.1 uwe }
148 1.1 uwe
149 1.1 uwe int
150 1.1 uwe btnopen(dev_t dev, int flag, int mode, struct lwp *l)
151 1.1 uwe {
152 1.1 uwe int error;
153 1.1 uwe
154 1.6 rmind error = RUN_ONCE(&btn_once, btn_init);
155 1.6 rmind if (error) {
156 1.6 rmind return error;
157 1.5 rmind }
158 1.5 rmind
159 1.1 uwe if (minor(dev) != 0) {
160 1.1 uwe return (ENODEV);
161 1.1 uwe }
162 1.1 uwe
163 1.6 rmind mutex_enter(&btn_event_queue_lock);
164 1.1 uwe if (btn_daemon != NULL) {
165 1.1 uwe error = EBUSY;
166 1.1 uwe } else {
167 1.1 uwe error = 0;
168 1.1 uwe btn_daemon = l;
169 1.1 uwe btn_event_queue_flush();
170 1.1 uwe }
171 1.6 rmind mutex_exit(&btn_event_queue_lock);
172 1.1 uwe
173 1.1 uwe return (error);
174 1.1 uwe }
175 1.1 uwe
176 1.1 uwe int
177 1.1 uwe btnclose(dev_t dev, int flag, int mode, struct lwp *l)
178 1.1 uwe {
179 1.1 uwe int count;
180 1.1 uwe
181 1.1 uwe if (minor(dev) != 0) {
182 1.1 uwe return (ENODEV);
183 1.1 uwe }
184 1.1 uwe
185 1.6 rmind mutex_enter(&btn_event_queue_lock);
186 1.1 uwe count = btn_event_queue_count;
187 1.1 uwe btn_daemon = NULL;
188 1.1 uwe btn_event_queue_flush();
189 1.6 rmind mutex_exit(&btn_event_queue_lock);
190 1.1 uwe
191 1.1 uwe if (count) {
192 1.1 uwe printf("WARNING: %d events lost by exiting daemon\n", count);
193 1.1 uwe }
194 1.1 uwe
195 1.1 uwe return (0);
196 1.1 uwe }
197 1.1 uwe
198 1.1 uwe int
199 1.2 christos btnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
200 1.1 uwe {
201 1.1 uwe int error = 0;
202 1.1 uwe
203 1.1 uwe if (minor(dev) != 0) {
204 1.1 uwe return (ENODEV);
205 1.1 uwe }
206 1.1 uwe
207 1.1 uwe switch (cmd) {
208 1.1 uwe case BUTTON_IOC_GET_TYPE:
209 1.1 uwe {
210 1.1 uwe struct button_type *button_type = (void *)data;
211 1.1 uwe strcpy(button_type->button_type, btn_type);
212 1.1 uwe break;
213 1.1 uwe }
214 1.1 uwe
215 1.1 uwe default:
216 1.1 uwe error = ENOTTY;
217 1.1 uwe break;
218 1.1 uwe }
219 1.1 uwe
220 1.1 uwe return (error);
221 1.1 uwe }
222 1.1 uwe
223 1.1 uwe int
224 1.1 uwe btnread(dev_t dev, struct uio *uio, int flags)
225 1.1 uwe {
226 1.1 uwe button_event_t bev;
227 1.1 uwe int error;
228 1.1 uwe
229 1.1 uwe if (minor(dev) != 0) {
230 1.1 uwe return (ENODEV);
231 1.1 uwe }
232 1.1 uwe
233 1.1 uwe if (uio->uio_resid != BUTTON_EVENT_MSG_SIZE) {
234 1.1 uwe return (EINVAL);
235 1.1 uwe }
236 1.1 uwe
237 1.6 rmind mutex_enter(&btn_event_queue_lock);
238 1.1 uwe for (;;) {
239 1.1 uwe if (btn_get_event(&bev)) {
240 1.6 rmind mutex_exit(&btn_event_queue_lock);
241 1.1 uwe return (uiomove(&bev, BUTTON_EVENT_MSG_SIZE, uio));
242 1.1 uwe }
243 1.1 uwe
244 1.1 uwe if (flags & IO_NDELAY) {
245 1.6 rmind mutex_exit(&btn_event_queue_lock);
246 1.1 uwe return (EWOULDBLOCK);
247 1.1 uwe }
248 1.1 uwe
249 1.1 uwe btn_event_queue_flags |= BEVQ_F_WAITING;
250 1.6 rmind error = cv_wait_sig(&btn_event_queue_cv, &btn_event_queue_lock);
251 1.1 uwe if (error) {
252 1.6 rmind mutex_exit(&btn_event_queue_lock);
253 1.1 uwe return (error);
254 1.1 uwe }
255 1.1 uwe }
256 1.1 uwe }
257 1.1 uwe
258 1.1 uwe int
259 1.1 uwe btnpoll(dev_t dev, int events, struct lwp *l)
260 1.1 uwe {
261 1.1 uwe int revents;
262 1.1 uwe
263 1.1 uwe if (minor(dev) != 0) {
264 1.1 uwe return (ENODEV);
265 1.1 uwe }
266 1.1 uwe
267 1.1 uwe revents = events & (POLLOUT | POLLWRNORM);
268 1.1 uwe
269 1.1 uwe /* Attempt to save some work. */
270 1.1 uwe if ((events & (POLLIN | POLLRDNORM)) == 0)
271 1.1 uwe return (revents);
272 1.1 uwe
273 1.6 rmind mutex_enter(&btn_event_queue_lock);
274 1.1 uwe if (btn_event_queue_count) {
275 1.1 uwe revents |= events & (POLLIN | POLLRDNORM);
276 1.1 uwe } else {
277 1.1 uwe selrecord(l, &btn_event_queue_selinfo);
278 1.1 uwe }
279 1.6 rmind mutex_exit(&btn_event_queue_lock);
280 1.1 uwe
281 1.1 uwe return (revents);
282 1.1 uwe }
283 1.1 uwe
284 1.1 uwe static void
285 1.1 uwe filt_btn_rdetach(struct knote *kn)
286 1.1 uwe {
287 1.1 uwe
288 1.6 rmind mutex_enter(&btn_event_queue_lock);
289 1.1 uwe SLIST_REMOVE(&btn_event_queue_selinfo.sel_klist,
290 1.1 uwe kn, knote, kn_selnext);
291 1.6 rmind mutex_exit(&btn_event_queue_lock);
292 1.1 uwe }
293 1.1 uwe
294 1.1 uwe static int
295 1.1 uwe filt_btn_read(struct knote *kn, long hint)
296 1.1 uwe {
297 1.1 uwe
298 1.6 rmind mutex_enter(&btn_event_queue_lock);
299 1.1 uwe kn->kn_data = btn_event_queue_count;
300 1.6 rmind mutex_exit(&btn_event_queue_lock);
301 1.1 uwe
302 1.1 uwe return (kn->kn_data > 0);
303 1.1 uwe }
304 1.1 uwe
305 1.1 uwe static const struct filterops btn_read_filtops =
306 1.1 uwe { 1, NULL, filt_btn_rdetach, filt_btn_read };
307 1.1 uwe
308 1.1 uwe static const struct filterops btn_write_filtops =
309 1.1 uwe { 1, NULL, filt_btn_rdetach, filt_seltrue };
310 1.1 uwe
311 1.1 uwe int
312 1.1 uwe btnkqfilter(dev_t dev, struct knote *kn)
313 1.1 uwe {
314 1.1 uwe struct klist *klist;
315 1.1 uwe
316 1.1 uwe if (minor(dev) != 0) {
317 1.1 uwe return (ENODEV);
318 1.1 uwe }
319 1.1 uwe
320 1.1 uwe switch (kn->kn_filter) {
321 1.1 uwe case EVFILT_READ:
322 1.1 uwe klist = &btn_event_queue_selinfo.sel_klist;
323 1.1 uwe kn->kn_fop = &btn_read_filtops;
324 1.1 uwe break;
325 1.1 uwe
326 1.1 uwe case EVFILT_WRITE:
327 1.1 uwe klist = &btn_event_queue_selinfo.sel_klist;
328 1.1 uwe kn->kn_fop = &btn_write_filtops;
329 1.1 uwe break;
330 1.1 uwe
331 1.1 uwe default:
332 1.1 uwe return (1);
333 1.1 uwe }
334 1.1 uwe
335 1.6 rmind mutex_enter(&btn_event_queue_lock);
336 1.1 uwe SLIST_INSERT_HEAD(klist, kn, kn_selnext);
337 1.6 rmind mutex_exit(&btn_event_queue_lock);
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