Home | History | Annotate | Line # | Download | only in dev
button.c revision 1.2.20.2
      1  1.2.20.2      matt /*	button.c,v 1.2.20.1 2008/01/09 01:46:57 matt 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.2.20.2      matt __KERNEL_RCSID(0, "button.c,v 1.2.20.1 2008/01/09 01:46:57 matt 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/malloc.h>
     45  1.2.20.1      matt #include <sys/simplelock.h>
     46       1.1       uwe #include <sys/queue.h>
     47       1.1       uwe #include <sys/proc.h>
     48       1.1       uwe #include <sys/kthread.h>
     49       1.1       uwe #include <sys/errno.h>
     50       1.1       uwe #include <sys/fcntl.h>
     51       1.1       uwe #include <sys/callout.h>
     52       1.1       uwe #include <sys/kernel.h>
     53       1.1       uwe #include <sys/poll.h>
     54       1.1       uwe #include <sys/select.h>
     55       1.1       uwe #include <sys/vnode.h>
     56       1.1       uwe 
     57       1.1       uwe #include <machine/button.h>
     58       1.1       uwe 
     59       1.1       uwe #include <landisk/dev/buttonvar.h>
     60       1.1       uwe 
     61       1.1       uwe /*
     62       1.1       uwe  * event handler
     63       1.1       uwe  */
     64       1.1       uwe static LIST_HEAD(, btn_event) btn_event_list =
     65       1.1       uwe     LIST_HEAD_INITIALIZER(btn_event_list);
     66       1.1       uwe static struct simplelock btn_event_list_slock =
     67       1.1       uwe     SIMPLELOCK_INITIALIZER;
     68       1.1       uwe 
     69       1.1       uwe static struct lwp *btn_daemon;
     70       1.1       uwe 
     71       1.1       uwe #define	BTN_MAX_EVENTS		32
     72       1.1       uwe 
     73       1.1       uwe static struct simplelock btn_event_queue_slock =
     74       1.1       uwe     SIMPLELOCK_INITIALIZER;
     75       1.1       uwe static button_event_t btn_event_queue[BTN_MAX_EVENTS];
     76       1.1       uwe static int btn_event_queue_head;
     77       1.1       uwe static int btn_event_queue_tail;
     78       1.1       uwe static int btn_event_queue_count;
     79       1.1       uwe static int btn_event_queue_flags;
     80       1.1       uwe static struct selinfo btn_event_queue_selinfo;
     81       1.1       uwe 
     82       1.1       uwe static char btn_type[32];
     83       1.1       uwe 
     84       1.1       uwe #define	BEVQ_F_WAITING		0x01	/* daemon waiting for event */
     85       1.1       uwe 
     86       1.1       uwe #define	BTN_NEXT_EVENT(x)	(((x) + 1) / BTN_MAX_EVENTS)
     87       1.1       uwe 
     88       1.1       uwe dev_type_open(btnopen);
     89       1.1       uwe dev_type_close(btnclose);
     90       1.1       uwe dev_type_ioctl(btnioctl);
     91       1.1       uwe dev_type_read(btnread);
     92       1.1       uwe dev_type_poll(btnpoll);
     93       1.1       uwe dev_type_kqfilter(btnkqfilter);
     94       1.1       uwe 
     95       1.1       uwe const struct cdevsw button_cdevsw = {
     96       1.1       uwe 	btnopen, btnclose, btnread, nowrite, btnioctl,
     97       1.1       uwe 	nostop, notty, btnpoll, nommap, btnkqfilter,
     98       1.1       uwe };
     99       1.1       uwe 
    100       1.1       uwe static int
    101       1.1       uwe btn_queue_event(button_event_t *bev)
    102       1.1       uwe {
    103       1.1       uwe 
    104       1.1       uwe 	if (btn_event_queue_count == BTN_MAX_EVENTS)
    105       1.1       uwe 		return (0);
    106       1.1       uwe 
    107       1.1       uwe 	btn_event_queue[btn_event_queue_head] = *bev;
    108       1.1       uwe 	btn_event_queue_head = BTN_NEXT_EVENT(btn_event_queue_head);
    109       1.1       uwe 	btn_event_queue_count++;
    110       1.1       uwe 
    111       1.1       uwe 	return (1);
    112       1.1       uwe }
    113       1.1       uwe 
    114       1.1       uwe static int
    115       1.1       uwe btn_get_event(button_event_t *bev)
    116       1.1       uwe {
    117       1.1       uwe 
    118       1.1       uwe 	if (btn_event_queue_count == 0)
    119       1.1       uwe 		return (0);
    120       1.1       uwe 
    121       1.1       uwe 	*bev = btn_event_queue[btn_event_queue_tail];
    122       1.1       uwe 	btn_event_queue_tail = BTN_NEXT_EVENT(btn_event_queue_tail);
    123       1.1       uwe 	btn_event_queue_count--;
    124       1.1       uwe 
    125       1.1       uwe 	return (1);
    126       1.1       uwe }
    127       1.1       uwe 
    128       1.1       uwe static void
    129       1.1       uwe btn_event_queue_flush(void)
    130       1.1       uwe {
    131       1.1       uwe 
    132       1.1       uwe 	btn_event_queue_head = 0;
    133       1.1       uwe 	btn_event_queue_tail = 0;
    134       1.1       uwe 	btn_event_queue_count = 0;
    135       1.1       uwe 	btn_event_queue_flags = 0;
    136       1.1       uwe }
    137       1.1       uwe 
    138       1.1       uwe int
    139       1.1       uwe btnopen(dev_t dev, int flag, int mode, struct lwp *l)
    140       1.1       uwe {
    141  1.2.20.2      matt 	static bool btn_event_queue_selinfo_init;	/* XXX */
    142       1.1       uwe 	int error;
    143       1.1       uwe 
    144  1.2.20.2      matt 	if (!btn_event_queue_selinfo_init) {
    145  1.2.20.2      matt 		selinit(&btn_event_queue_selinfo);
    146  1.2.20.2      matt 		btn_event_queue_selinfo_init = true;
    147  1.2.20.2      matt 	}
    148  1.2.20.2      matt 
    149       1.1       uwe 	if (minor(dev) != 0) {
    150       1.1       uwe 		return (ENODEV);
    151       1.1       uwe 	}
    152       1.1       uwe 
    153       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    154       1.1       uwe 	if (btn_daemon != NULL) {
    155       1.1       uwe 		error = EBUSY;
    156       1.1       uwe 	} else {
    157       1.1       uwe 		error = 0;
    158       1.1       uwe 		btn_daemon = l;
    159       1.1       uwe 		btn_event_queue_flush();
    160       1.1       uwe 	}
    161       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    162       1.1       uwe 
    163       1.1       uwe 	return (error);
    164       1.1       uwe }
    165       1.1       uwe 
    166       1.1       uwe int
    167       1.1       uwe btnclose(dev_t dev, int flag, int mode, struct lwp *l)
    168       1.1       uwe {
    169       1.1       uwe 	int count;
    170       1.1       uwe 
    171       1.1       uwe 	if (minor(dev) != 0) {
    172       1.1       uwe 		return (ENODEV);
    173       1.1       uwe 	}
    174       1.1       uwe 
    175       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    176       1.1       uwe 	count = btn_event_queue_count;
    177       1.1       uwe 	btn_daemon = NULL;
    178       1.1       uwe 	btn_event_queue_flush();
    179       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    180       1.1       uwe 
    181       1.1       uwe 	if (count) {
    182       1.1       uwe 		printf("WARNING: %d events lost by exiting daemon\n", count);
    183       1.1       uwe 	}
    184       1.1       uwe 
    185       1.1       uwe 	return (0);
    186       1.1       uwe }
    187       1.1       uwe 
    188       1.1       uwe int
    189       1.2  christos btnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    190       1.1       uwe {
    191       1.1       uwe 	int error = 0;
    192       1.1       uwe 
    193       1.1       uwe 	if (minor(dev) != 0) {
    194       1.1       uwe 		return (ENODEV);
    195       1.1       uwe 	}
    196       1.1       uwe 
    197       1.1       uwe 	switch (cmd) {
    198       1.1       uwe 	case BUTTON_IOC_GET_TYPE:
    199       1.1       uwe 	    {
    200       1.1       uwe 		struct button_type *button_type = (void *)data;
    201       1.1       uwe 		strcpy(button_type->button_type, btn_type);
    202       1.1       uwe 		break;
    203       1.1       uwe 	    }
    204       1.1       uwe 
    205       1.1       uwe 	default:
    206       1.1       uwe 		error = ENOTTY;
    207       1.1       uwe 		break;
    208       1.1       uwe 	}
    209       1.1       uwe 
    210       1.1       uwe 	return (error);
    211       1.1       uwe }
    212       1.1       uwe 
    213       1.1       uwe int
    214       1.1       uwe btnread(dev_t dev, struct uio *uio, int flags)
    215       1.1       uwe {
    216       1.1       uwe 	button_event_t bev;
    217       1.1       uwe 	int error;
    218       1.1       uwe 
    219       1.1       uwe 	if (minor(dev) != 0) {
    220       1.1       uwe 		return (ENODEV);
    221       1.1       uwe 	}
    222       1.1       uwe 
    223       1.1       uwe 	if (uio->uio_resid != BUTTON_EVENT_MSG_SIZE) {
    224       1.1       uwe 		return (EINVAL);
    225       1.1       uwe 	}
    226       1.1       uwe 
    227       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    228       1.1       uwe 	for (;;) {
    229       1.1       uwe 		if (btn_get_event(&bev)) {
    230       1.1       uwe 			simple_unlock(&btn_event_queue_slock);
    231       1.1       uwe 			return (uiomove(&bev, BUTTON_EVENT_MSG_SIZE, uio));
    232       1.1       uwe 		}
    233       1.1       uwe 
    234       1.1       uwe 		if (flags & IO_NDELAY) {
    235       1.1       uwe 			simple_unlock(&btn_event_queue_slock);
    236       1.1       uwe 			return (EWOULDBLOCK);
    237       1.1       uwe 		}
    238       1.1       uwe 
    239       1.1       uwe 		btn_event_queue_flags |= BEVQ_F_WAITING;
    240       1.1       uwe 		error = ltsleep(&btn_event_queue_count,
    241       1.1       uwe 		    (PRIBIO|PCATCH), "btnread", 0, &btn_event_queue_slock);
    242       1.1       uwe 		if (error) {
    243       1.1       uwe 			simple_unlock(&btn_event_queue_slock);
    244       1.1       uwe 			return (error);
    245       1.1       uwe 		}
    246       1.1       uwe 	}
    247       1.1       uwe }
    248       1.1       uwe 
    249       1.1       uwe int
    250       1.1       uwe btnpoll(dev_t dev, int events, struct lwp *l)
    251       1.1       uwe {
    252       1.1       uwe 	int revents;
    253       1.1       uwe 
    254       1.1       uwe 	if (minor(dev) != 0) {
    255       1.1       uwe 		return (ENODEV);
    256       1.1       uwe 	}
    257       1.1       uwe 
    258       1.1       uwe 	revents = events & (POLLOUT | POLLWRNORM);
    259       1.1       uwe 
    260       1.1       uwe 	/* Attempt to save some work. */
    261       1.1       uwe 	if ((events & (POLLIN | POLLRDNORM)) == 0)
    262       1.1       uwe 		return (revents);
    263       1.1       uwe 
    264       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    265       1.1       uwe 	if (btn_event_queue_count) {
    266       1.1       uwe 		revents |= events & (POLLIN | POLLRDNORM);
    267       1.1       uwe 	} else {
    268       1.1       uwe 		selrecord(l, &btn_event_queue_selinfo);
    269       1.1       uwe 	}
    270       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    271       1.1       uwe 
    272       1.1       uwe 	return (revents);
    273       1.1       uwe }
    274       1.1       uwe 
    275       1.1       uwe static void
    276       1.1       uwe filt_btn_rdetach(struct knote *kn)
    277       1.1       uwe {
    278       1.1       uwe 
    279       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    280       1.1       uwe 	SLIST_REMOVE(&btn_event_queue_selinfo.sel_klist,
    281       1.1       uwe 	    kn, knote, kn_selnext);
    282       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    283       1.1       uwe }
    284       1.1       uwe 
    285       1.1       uwe static int
    286       1.1       uwe filt_btn_read(struct knote *kn, long hint)
    287       1.1       uwe {
    288       1.1       uwe 
    289       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    290       1.1       uwe 	kn->kn_data = btn_event_queue_count;
    291       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    292       1.1       uwe 
    293       1.1       uwe 	return (kn->kn_data > 0);
    294       1.1       uwe }
    295       1.1       uwe 
    296       1.1       uwe static const struct filterops btn_read_filtops =
    297       1.1       uwe     { 1, NULL, filt_btn_rdetach, filt_btn_read };
    298       1.1       uwe 
    299       1.1       uwe static const struct filterops btn_write_filtops =
    300       1.1       uwe     { 1, NULL, filt_btn_rdetach, filt_seltrue };
    301       1.1       uwe 
    302       1.1       uwe int
    303       1.1       uwe btnkqfilter(dev_t dev, struct knote *kn)
    304       1.1       uwe {
    305       1.1       uwe 	struct klist *klist;
    306       1.1       uwe 
    307       1.1       uwe 	if (minor(dev) != 0) {
    308       1.1       uwe 		return (ENODEV);
    309       1.1       uwe 	}
    310       1.1       uwe 
    311       1.1       uwe 	switch (kn->kn_filter) {
    312       1.1       uwe 	case EVFILT_READ:
    313       1.1       uwe 		klist = &btn_event_queue_selinfo.sel_klist;
    314       1.1       uwe 		kn->kn_fop = &btn_read_filtops;
    315       1.1       uwe 		break;
    316       1.1       uwe 
    317       1.1       uwe 	case EVFILT_WRITE:
    318       1.1       uwe 		klist = &btn_event_queue_selinfo.sel_klist;
    319       1.1       uwe 		kn->kn_fop = &btn_write_filtops;
    320       1.1       uwe 		break;
    321       1.1       uwe 
    322       1.1       uwe 	default:
    323       1.1       uwe 		return (1);
    324       1.1       uwe 	}
    325       1.1       uwe 
    326       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    327       1.1       uwe 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    328       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    329       1.1       uwe 
    330       1.1       uwe 	return (0);
    331       1.1       uwe }
    332       1.1       uwe 
    333       1.1       uwe void
    334       1.1       uwe btn_settype(const char *type)
    335       1.1       uwe {
    336       1.1       uwe 
    337       1.1       uwe 	/*
    338       1.1       uwe 	 * Don't bother locking this; it's going to be set
    339       1.1       uwe 	 * during autoconfiguration, and then only read from
    340       1.1       uwe 	 * then on.
    341       1.1       uwe 	 */
    342       1.1       uwe 	strlcpy(btn_type, type, sizeof(btn_type));
    343       1.1       uwe }
    344       1.1       uwe 
    345       1.1       uwe int
    346       1.1       uwe btn_event_register(struct btn_event *bev)
    347       1.1       uwe {
    348       1.1       uwe 
    349       1.1       uwe 	simple_lock(&btn_event_list_slock);
    350       1.1       uwe 	LIST_INSERT_HEAD(&btn_event_list, bev, bev_list);
    351       1.1       uwe 	simple_unlock(&btn_event_list_slock);
    352       1.1       uwe 
    353       1.1       uwe 	return (0);
    354       1.1       uwe }
    355       1.1       uwe 
    356       1.1       uwe void
    357       1.1       uwe btn_event_unregister(struct btn_event *bev)
    358       1.1       uwe {
    359       1.1       uwe 
    360       1.1       uwe 	simple_lock(&btn_event_list_slock);
    361       1.1       uwe 	LIST_REMOVE(bev, bev_list);
    362       1.1       uwe 	simple_unlock(&btn_event_list_slock);
    363       1.1       uwe }
    364       1.1       uwe 
    365       1.1       uwe void
    366       1.1       uwe btn_event_send(struct btn_event *bev, int event)
    367       1.1       uwe {
    368       1.1       uwe 	button_event_t btnev;
    369       1.1       uwe 	int rv;
    370       1.1       uwe 
    371       1.1       uwe 	simple_lock(&btn_event_queue_slock);
    372       1.1       uwe 	if (btn_daemon != NULL) {
    373       1.1       uwe 		btnev.bev_type = BUTTON_EVENT_STATE_CHANGE;
    374       1.1       uwe 		btnev.bev_event.bs_state = event;
    375       1.1       uwe 		strcpy(btnev.bev_event.bs_name, bev->bev_name);
    376       1.1       uwe 
    377       1.1       uwe 		rv = btn_queue_event(&btnev);
    378       1.1       uwe 		if (rv == 0) {
    379       1.1       uwe 			simple_unlock(&btn_event_queue_slock);
    380       1.1       uwe 			printf("%s: WARNING: state change event %d lost; "
    381       1.1       uwe 			    "queue full\n", bev->bev_name, btnev.bev_type);
    382       1.1       uwe 		} else {
    383       1.1       uwe 			if (btn_event_queue_flags & BEVQ_F_WAITING) {
    384       1.1       uwe 				btn_event_queue_flags &= ~BEVQ_F_WAITING;
    385       1.1       uwe 				simple_unlock(&btn_event_queue_slock);
    386       1.1       uwe 				wakeup(&btn_event_queue_count);
    387       1.1       uwe 			} else {
    388       1.1       uwe 				simple_unlock(&btn_event_queue_slock);
    389       1.1       uwe 			}
    390  1.2.20.2      matt 			selnotify(&btn_event_queue_selinfo, 0, 0);
    391       1.1       uwe 		}
    392       1.1       uwe 		return;
    393       1.1       uwe 	}
    394       1.1       uwe 	simple_unlock(&btn_event_queue_slock);
    395       1.1       uwe 
    396       1.1       uwe 	printf("%s: btn_event_send can't handle me.\n", bev->bev_name);
    397       1.1       uwe }
    398