Home | History | Annotate | Line # | Download | only in dev
button.c revision 1.6.14.2
      1  1.6.14.1       tls /*	$NetBSD: button.c,v 1.6.14.2 2017/12/03 11:36:22 jdolecek 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.14.1       tls __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.6.14.2 2017/12/03 11:36:22 jdolecek 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.6.14.1       tls 	.d_open = btnopen,
     95  1.6.14.1       tls 	.d_close = btnclose,
     96  1.6.14.1       tls 	.d_read = btnread,
     97  1.6.14.1       tls 	.d_write = nowrite,
     98  1.6.14.1       tls 	.d_ioctl = btnioctl,
     99  1.6.14.1       tls 	.d_stop = nostop,
    100  1.6.14.1       tls 	.d_tty = notty,
    101  1.6.14.1       tls 	.d_poll = btnpoll,
    102  1.6.14.1       tls 	.d_mmap = nommap,
    103  1.6.14.1       tls 	.d_kqfilter = btnkqfilter,
    104  1.6.14.1       tls 	.d_discard = nodiscard,
    105  1.6.14.1       tls 	.d_flag = 0
    106       1.1       uwe };
    107       1.1       uwe 
    108       1.1       uwe static int
    109       1.6     rmind btn_init(void)
    110       1.6     rmind {
    111       1.6     rmind 
    112       1.6     rmind 	LIST_INIT(&btn_event_list);
    113       1.6     rmind 	mutex_init(&btn_event_list_lock, MUTEX_DEFAULT, IPL_NONE);
    114       1.6     rmind 	mutex_init(&btn_event_queue_lock, MUTEX_DEFAULT, IPL_NONE);
    115       1.6     rmind 	cv_init(&btn_event_queue_cv, "btncv");
    116       1.6     rmind 	selinit(&btn_event_queue_selinfo);
    117       1.6     rmind 
    118       1.6     rmind 	return 0;
    119       1.6     rmind }
    120       1.6     rmind 
    121       1.6     rmind static int
    122       1.1       uwe btn_queue_event(button_event_t *bev)
    123       1.1       uwe {
    124       1.1       uwe 
    125       1.1       uwe 	if (btn_event_queue_count == BTN_MAX_EVENTS)
    126       1.1       uwe 		return (0);
    127       1.1       uwe 
    128       1.1       uwe 	btn_event_queue[btn_event_queue_head] = *bev;
    129       1.1       uwe 	btn_event_queue_head = BTN_NEXT_EVENT(btn_event_queue_head);
    130       1.1       uwe 	btn_event_queue_count++;
    131       1.1       uwe 
    132       1.1       uwe 	return (1);
    133       1.1       uwe }
    134       1.1       uwe 
    135       1.1       uwe static int
    136       1.1       uwe btn_get_event(button_event_t *bev)
    137       1.1       uwe {
    138       1.1       uwe 
    139       1.1       uwe 	if (btn_event_queue_count == 0)
    140       1.1       uwe 		return (0);
    141       1.1       uwe 
    142       1.1       uwe 	*bev = btn_event_queue[btn_event_queue_tail];
    143       1.1       uwe 	btn_event_queue_tail = BTN_NEXT_EVENT(btn_event_queue_tail);
    144       1.1       uwe 	btn_event_queue_count--;
    145       1.1       uwe 
    146       1.1       uwe 	return (1);
    147       1.1       uwe }
    148       1.1       uwe 
    149       1.1       uwe static void
    150       1.1       uwe btn_event_queue_flush(void)
    151       1.1       uwe {
    152       1.1       uwe 
    153       1.1       uwe 	btn_event_queue_head = 0;
    154       1.1       uwe 	btn_event_queue_tail = 0;
    155       1.1       uwe 	btn_event_queue_count = 0;
    156       1.1       uwe 	btn_event_queue_flags = 0;
    157       1.1       uwe }
    158       1.1       uwe 
    159       1.1       uwe int
    160       1.1       uwe btnopen(dev_t dev, int flag, int mode, struct lwp *l)
    161       1.1       uwe {
    162       1.1       uwe 	int error;
    163       1.1       uwe 
    164       1.6     rmind 	error = RUN_ONCE(&btn_once, btn_init);
    165       1.6     rmind 	if (error) {
    166       1.6     rmind 		return error;
    167       1.5     rmind 	}
    168       1.5     rmind 
    169       1.1       uwe 	if (minor(dev) != 0) {
    170       1.1       uwe 		return (ENODEV);
    171       1.1       uwe 	}
    172       1.1       uwe 
    173       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    174       1.1       uwe 	if (btn_daemon != NULL) {
    175       1.1       uwe 		error = EBUSY;
    176       1.1       uwe 	} else {
    177       1.1       uwe 		error = 0;
    178       1.1       uwe 		btn_daemon = l;
    179       1.1       uwe 		btn_event_queue_flush();
    180       1.1       uwe 	}
    181       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    182       1.1       uwe 
    183       1.1       uwe 	return (error);
    184       1.1       uwe }
    185       1.1       uwe 
    186       1.1       uwe int
    187       1.1       uwe btnclose(dev_t dev, int flag, int mode, struct lwp *l)
    188       1.1       uwe {
    189       1.1       uwe 	int count;
    190       1.1       uwe 
    191       1.1       uwe 	if (minor(dev) != 0) {
    192       1.1       uwe 		return (ENODEV);
    193       1.1       uwe 	}
    194       1.1       uwe 
    195       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    196       1.1       uwe 	count = btn_event_queue_count;
    197       1.1       uwe 	btn_daemon = NULL;
    198       1.1       uwe 	btn_event_queue_flush();
    199       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    200       1.1       uwe 
    201       1.1       uwe 	if (count) {
    202       1.1       uwe 		printf("WARNING: %d events lost by exiting daemon\n", count);
    203       1.1       uwe 	}
    204       1.1       uwe 
    205       1.1       uwe 	return (0);
    206       1.1       uwe }
    207       1.1       uwe 
    208       1.1       uwe int
    209       1.2  christos btnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    210       1.1       uwe {
    211       1.1       uwe 	int error = 0;
    212       1.1       uwe 
    213       1.1       uwe 	if (minor(dev) != 0) {
    214       1.1       uwe 		return (ENODEV);
    215       1.1       uwe 	}
    216       1.1       uwe 
    217       1.1       uwe 	switch (cmd) {
    218       1.1       uwe 	case BUTTON_IOC_GET_TYPE:
    219       1.1       uwe 	    {
    220       1.1       uwe 		struct button_type *button_type = (void *)data;
    221       1.1       uwe 		strcpy(button_type->button_type, btn_type);
    222       1.1       uwe 		break;
    223       1.1       uwe 	    }
    224       1.1       uwe 
    225       1.1       uwe 	default:
    226       1.1       uwe 		error = ENOTTY;
    227       1.1       uwe 		break;
    228       1.1       uwe 	}
    229       1.1       uwe 
    230       1.1       uwe 	return (error);
    231       1.1       uwe }
    232       1.1       uwe 
    233       1.1       uwe int
    234       1.1       uwe btnread(dev_t dev, struct uio *uio, int flags)
    235       1.1       uwe {
    236       1.1       uwe 	button_event_t bev;
    237       1.1       uwe 	int error;
    238       1.1       uwe 
    239       1.1       uwe 	if (minor(dev) != 0) {
    240       1.1       uwe 		return (ENODEV);
    241       1.1       uwe 	}
    242       1.1       uwe 
    243       1.1       uwe 	if (uio->uio_resid != BUTTON_EVENT_MSG_SIZE) {
    244       1.1       uwe 		return (EINVAL);
    245       1.1       uwe 	}
    246       1.1       uwe 
    247       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    248       1.1       uwe 	for (;;) {
    249       1.1       uwe 		if (btn_get_event(&bev)) {
    250       1.6     rmind 			mutex_exit(&btn_event_queue_lock);
    251       1.1       uwe 			return (uiomove(&bev, BUTTON_EVENT_MSG_SIZE, uio));
    252       1.1       uwe 		}
    253       1.1       uwe 
    254       1.1       uwe 		if (flags & IO_NDELAY) {
    255       1.6     rmind 			mutex_exit(&btn_event_queue_lock);
    256       1.1       uwe 			return (EWOULDBLOCK);
    257       1.1       uwe 		}
    258       1.1       uwe 
    259       1.1       uwe 		btn_event_queue_flags |= BEVQ_F_WAITING;
    260       1.6     rmind 		error = cv_wait_sig(&btn_event_queue_cv, &btn_event_queue_lock);
    261       1.1       uwe 		if (error) {
    262       1.6     rmind 			mutex_exit(&btn_event_queue_lock);
    263       1.1       uwe 			return (error);
    264       1.1       uwe 		}
    265       1.1       uwe 	}
    266       1.1       uwe }
    267       1.1       uwe 
    268       1.1       uwe int
    269       1.1       uwe btnpoll(dev_t dev, int events, struct lwp *l)
    270       1.1       uwe {
    271       1.1       uwe 	int revents;
    272       1.1       uwe 
    273       1.1       uwe 	if (minor(dev) != 0) {
    274       1.1       uwe 		return (ENODEV);
    275       1.1       uwe 	}
    276       1.1       uwe 
    277       1.1       uwe 	revents = events & (POLLOUT | POLLWRNORM);
    278       1.1       uwe 
    279       1.1       uwe 	/* Attempt to save some work. */
    280       1.1       uwe 	if ((events & (POLLIN | POLLRDNORM)) == 0)
    281       1.1       uwe 		return (revents);
    282       1.1       uwe 
    283       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    284       1.1       uwe 	if (btn_event_queue_count) {
    285       1.1       uwe 		revents |= events & (POLLIN | POLLRDNORM);
    286       1.1       uwe 	} else {
    287       1.1       uwe 		selrecord(l, &btn_event_queue_selinfo);
    288       1.1       uwe 	}
    289       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    290       1.1       uwe 
    291       1.1       uwe 	return (revents);
    292       1.1       uwe }
    293       1.1       uwe 
    294       1.1       uwe static void
    295       1.1       uwe filt_btn_rdetach(struct knote *kn)
    296       1.1       uwe {
    297       1.1       uwe 
    298       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    299       1.1       uwe 	SLIST_REMOVE(&btn_event_queue_selinfo.sel_klist,
    300       1.1       uwe 	    kn, knote, kn_selnext);
    301       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    302       1.1       uwe }
    303       1.1       uwe 
    304       1.1       uwe static int
    305       1.1       uwe filt_btn_read(struct knote *kn, long hint)
    306       1.1       uwe {
    307       1.1       uwe 
    308       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    309       1.1       uwe 	kn->kn_data = btn_event_queue_count;
    310       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    311       1.1       uwe 
    312       1.1       uwe 	return (kn->kn_data > 0);
    313       1.1       uwe }
    314       1.1       uwe 
    315  1.6.14.2  jdolecek static const struct filterops btn_read_filtops = {
    316  1.6.14.2  jdolecek     .f_isfd = 1,
    317  1.6.14.2  jdolecek     .f_attach = NULL,
    318  1.6.14.2  jdolecek     .f_detach = filt_btn_rdetach,
    319  1.6.14.2  jdolecek     .f_event = filt_btn_read,
    320  1.6.14.2  jdolecek };
    321  1.6.14.2  jdolecek 
    322  1.6.14.2  jdolecek static const struct filterops btn_write_filtops = {
    323  1.6.14.2  jdolecek     .f_isfd = 1,
    324  1.6.14.2  jdolecek     .f_attach = NULL,
    325  1.6.14.2  jdolecek     .f_detach = filt_btn_rdetach,
    326  1.6.14.2  jdolecek     .f_event = filt_seltrue,
    327  1.6.14.2  jdolecek };
    328       1.1       uwe 
    329       1.1       uwe int
    330       1.1       uwe btnkqfilter(dev_t dev, struct knote *kn)
    331       1.1       uwe {
    332       1.1       uwe 	struct klist *klist;
    333       1.1       uwe 
    334       1.1       uwe 	if (minor(dev) != 0) {
    335       1.1       uwe 		return (ENODEV);
    336       1.1       uwe 	}
    337       1.1       uwe 
    338       1.1       uwe 	switch (kn->kn_filter) {
    339       1.1       uwe 	case EVFILT_READ:
    340       1.1       uwe 		klist = &btn_event_queue_selinfo.sel_klist;
    341       1.1       uwe 		kn->kn_fop = &btn_read_filtops;
    342       1.1       uwe 		break;
    343       1.1       uwe 
    344       1.1       uwe 	case EVFILT_WRITE:
    345       1.1       uwe 		klist = &btn_event_queue_selinfo.sel_klist;
    346       1.1       uwe 		kn->kn_fop = &btn_write_filtops;
    347       1.1       uwe 		break;
    348       1.1       uwe 
    349       1.1       uwe 	default:
    350       1.1       uwe 		return (1);
    351       1.1       uwe 	}
    352       1.1       uwe 
    353       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    354       1.1       uwe 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    355       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    356       1.1       uwe 
    357       1.1       uwe 	return (0);
    358       1.1       uwe }
    359       1.1       uwe 
    360       1.1       uwe void
    361       1.1       uwe btn_settype(const char *type)
    362       1.1       uwe {
    363       1.1       uwe 
    364       1.1       uwe 	/*
    365       1.1       uwe 	 * Don't bother locking this; it's going to be set
    366       1.1       uwe 	 * during autoconfiguration, and then only read from
    367       1.1       uwe 	 * then on.
    368       1.1       uwe 	 */
    369       1.1       uwe 	strlcpy(btn_type, type, sizeof(btn_type));
    370       1.1       uwe }
    371       1.1       uwe 
    372       1.1       uwe int
    373       1.1       uwe btn_event_register(struct btn_event *bev)
    374       1.1       uwe {
    375       1.1       uwe 
    376       1.6     rmind 	mutex_enter(&btn_event_list_lock);
    377       1.1       uwe 	LIST_INSERT_HEAD(&btn_event_list, bev, bev_list);
    378       1.6     rmind 	mutex_exit(&btn_event_list_lock);
    379       1.1       uwe 
    380       1.1       uwe 	return (0);
    381       1.1       uwe }
    382       1.1       uwe 
    383       1.1       uwe void
    384       1.1       uwe btn_event_unregister(struct btn_event *bev)
    385       1.1       uwe {
    386       1.1       uwe 
    387       1.6     rmind 	mutex_enter(&btn_event_list_lock);
    388       1.1       uwe 	LIST_REMOVE(bev, bev_list);
    389       1.6     rmind 	mutex_exit(&btn_event_list_lock);
    390       1.1       uwe }
    391       1.1       uwe 
    392       1.1       uwe void
    393       1.1       uwe btn_event_send(struct btn_event *bev, int event)
    394       1.1       uwe {
    395       1.1       uwe 	button_event_t btnev;
    396       1.1       uwe 	int rv;
    397       1.1       uwe 
    398       1.6     rmind 	mutex_enter(&btn_event_queue_lock);
    399       1.6     rmind 	if (btn_daemon == NULL) {
    400       1.6     rmind 		mutex_exit(&btn_event_queue_lock);
    401       1.6     rmind 		printf("%s: btn_event_send can't handle me.\n", bev->bev_name);
    402       1.1       uwe 		return;
    403       1.1       uwe 	}
    404       1.1       uwe 
    405       1.6     rmind 	btnev.bev_type = BUTTON_EVENT_STATE_CHANGE;
    406       1.6     rmind 	btnev.bev_event.bs_state = event;
    407       1.6     rmind 	strcpy(btnev.bev_event.bs_name, bev->bev_name);
    408       1.6     rmind 
    409       1.6     rmind 	rv = btn_queue_event(&btnev);
    410       1.6     rmind 	if (rv == 0) {
    411       1.6     rmind 		mutex_exit(&btn_event_queue_lock);
    412       1.6     rmind 		printf("%s: WARNING: state change event %d lost; "
    413       1.6     rmind 		    "queue full\n", bev->bev_name, btnev.bev_type);
    414       1.6     rmind 		return;
    415       1.6     rmind 	}
    416       1.6     rmind 	if (btn_event_queue_flags & BEVQ_F_WAITING) {
    417       1.6     rmind 		btn_event_queue_flags &= ~BEVQ_F_WAITING;
    418       1.6     rmind 		cv_broadcast(&btn_event_queue_cv);
    419       1.6     rmind 	}
    420       1.6     rmind 	selnotify(&btn_event_queue_selinfo, 0, 0);
    421       1.6     rmind 	mutex_exit(&btn_event_queue_lock);
    422       1.1       uwe }
    423