Home | History | Annotate | Line # | Download | only in netwalker
netwalker_btn.c revision 1.2.2.2
      1  1.2.2.2  rmind /*	$NetBSD: netwalker_btn.c,v 1.2.2.2 2014/05/18 17:45:05 rmind Exp $	*/
      2  1.2.2.2  rmind 
      3  1.2.2.2  rmind /*
      4  1.2.2.2  rmind  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
      5  1.2.2.2  rmind  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  1.2.2.2  rmind  *
      7  1.2.2.2  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  rmind  * modification, are permitted provided that the following conditions
      9  1.2.2.2  rmind  * are met:
     10  1.2.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  rmind  *
     16  1.2.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     17  1.2.2.2  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  1.2.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2.2.2  rmind  */
     28  1.2.2.2  rmind 
     29  1.2.2.2  rmind #include <sys/cdefs.h>
     30  1.2.2.2  rmind __KERNEL_RCSID(0, "$NetBSD: netwalker_btn.c,v 1.2.2.2 2014/05/18 17:45:05 rmind Exp $");
     31  1.2.2.2  rmind 
     32  1.2.2.2  rmind #include "opt_imxspi.h"
     33  1.2.2.2  rmind #include "opt_mousebtn.h"
     34  1.2.2.2  rmind 
     35  1.2.2.2  rmind #include <sys/param.h>
     36  1.2.2.2  rmind #include <sys/systm.h>
     37  1.2.2.2  rmind #include <sys/kernel.h>
     38  1.2.2.2  rmind #include <sys/device.h>
     39  1.2.2.2  rmind #include <sys/lock.h>
     40  1.2.2.2  rmind #include <sys/callout.h>
     41  1.2.2.2  rmind #include <sys/gpio.h>
     42  1.2.2.2  rmind #include <sys/bus.h>
     43  1.2.2.2  rmind #include <sys/mutex.h>
     44  1.2.2.2  rmind 
     45  1.2.2.2  rmind #include <dev/wscons/wsconsio.h>
     46  1.2.2.2  rmind #include <dev/wscons/wsmousevar.h>
     47  1.2.2.2  rmind 
     48  1.2.2.2  rmind #include <dev/gpio/gpiovar.h>
     49  1.2.2.2  rmind 
     50  1.2.2.2  rmind #define GPIO1_BASE		160
     51  1.2.2.2  rmind 
     52  1.2.2.2  rmind #define MOUSEBTN_PIN_LEFT	0
     53  1.2.2.2  rmind #define MOUSEBTN_PIN_RIGHT	1
     54  1.2.2.2  rmind #define MOUSEBTN_NPINS		2
     55  1.2.2.2  rmind 
     56  1.2.2.2  rmind #define POLLRATE (hz/10)
     57  1.2.2.2  rmind 
     58  1.2.2.2  rmind struct mousebtn_softc
     59  1.2.2.2  rmind {
     60  1.2.2.2  rmind 	device_t sc_dev;
     61  1.2.2.2  rmind 	void *sc_gpio;
     62  1.2.2.2  rmind 	void *sc_intr[MOUSEBTN_NPINS];
     63  1.2.2.2  rmind 
     64  1.2.2.2  rmind 	struct gpio_pinmap sc_map;
     65  1.2.2.2  rmind 	int sc_map_pins[MOUSEBTN_NPINS];
     66  1.2.2.2  rmind 
     67  1.2.2.2  rmind 	int sc_buttons;
     68  1.2.2.2  rmind 
     69  1.2.2.2  rmind 	struct callout sc_c;
     70  1.2.2.2  rmind 	kmutex_t sc_lock;
     71  1.2.2.2  rmind 
     72  1.2.2.2  rmind 	int sc_enabled;
     73  1.2.2.2  rmind 	struct device *sc_wsmousedev;
     74  1.2.2.2  rmind };
     75  1.2.2.2  rmind 
     76  1.2.2.2  rmind static int mousebtn_match(device_t, cfdata_t, void *);
     77  1.2.2.2  rmind static void mousebtn_attach(device_t, device_t, void *);
     78  1.2.2.2  rmind static int mousebtn_detach(device_t, int);
     79  1.2.2.2  rmind 
     80  1.2.2.2  rmind CFATTACH_DECL_NEW(netwalker_btn, sizeof(struct mousebtn_softc),
     81  1.2.2.2  rmind     mousebtn_match, mousebtn_attach, mousebtn_detach, NULL);
     82  1.2.2.2  rmind 
     83  1.2.2.2  rmind static void mousebtn_poll(void *);
     84  1.2.2.2  rmind static int mousebtn_intr(void *);
     85  1.2.2.2  rmind 
     86  1.2.2.2  rmind static int mousebtn_enable(void *v);
     87  1.2.2.2  rmind static void mousebtn_disable(void *v);
     88  1.2.2.2  rmind static int mousebtn_ioctl(void *, u_long, void *, int, struct lwp *);
     89  1.2.2.2  rmind 
     90  1.2.2.2  rmind static bool mousebtn_resume(device_t, const pmf_qual_t *);
     91  1.2.2.2  rmind static bool mousebtn_suspend(device_t, const pmf_qual_t *);
     92  1.2.2.2  rmind 
     93  1.2.2.2  rmind static const struct wsmouse_accessops mousebtn_accessops = {
     94  1.2.2.2  rmind 	.enable	 = mousebtn_enable,
     95  1.2.2.2  rmind 	.ioctl	 = mousebtn_ioctl,
     96  1.2.2.2  rmind 	.disable = mousebtn_disable
     97  1.2.2.2  rmind };
     98  1.2.2.2  rmind 
     99  1.2.2.2  rmind static int
    100  1.2.2.2  rmind mousebtn_match(device_t parent, cfdata_t cf, void * aux)
    101  1.2.2.2  rmind {
    102  1.2.2.2  rmind 	struct gpio_attach_args *ga = aux;
    103  1.2.2.2  rmind 
    104  1.2.2.2  rmind 	if (strcmp(ga->ga_dvname, cf->cf_name))
    105  1.2.2.2  rmind 		return 0;
    106  1.2.2.2  rmind 	if (ga->ga_offset == -1)
    107  1.2.2.2  rmind 		return 0;
    108  1.2.2.2  rmind 
    109  1.2.2.2  rmind 	/* check that we have enough pins */
    110  1.2.2.2  rmind 	if (gpio_npins(ga->ga_mask) != MOUSEBTN_NPINS) {
    111  1.2.2.2  rmind 		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
    112  1.2.2.2  rmind 		    ga->ga_mask);
    113  1.2.2.2  rmind 		return 0;
    114  1.2.2.2  rmind 	}
    115  1.2.2.2  rmind 
    116  1.2.2.2  rmind 	return 1;
    117  1.2.2.2  rmind }
    118  1.2.2.2  rmind 
    119  1.2.2.2  rmind static void
    120  1.2.2.2  rmind mousebtn_attach(device_t parent, device_t self, void *aux)
    121  1.2.2.2  rmind {
    122  1.2.2.2  rmind 	struct mousebtn_softc *sc = device_private(self);
    123  1.2.2.2  rmind 	struct gpio_attach_args *ga = aux;
    124  1.2.2.2  rmind 	int caps;
    125  1.2.2.2  rmind 	struct wsmousedev_attach_args a;
    126  1.2.2.2  rmind 
    127  1.2.2.2  rmind 	sc->sc_dev = self;
    128  1.2.2.2  rmind 	sc->sc_gpio = ga->ga_gpio;
    129  1.2.2.2  rmind 
    130  1.2.2.2  rmind 	/* map pins */
    131  1.2.2.2  rmind 	sc->sc_map.pm_map = sc->sc_map_pins;
    132  1.2.2.2  rmind 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
    133  1.2.2.2  rmind 		&sc->sc_map)) {
    134  1.2.2.2  rmind 		aprint_error(": couldn't map the pins\n");
    135  1.2.2.2  rmind 		return;
    136  1.2.2.2  rmind 	}
    137  1.2.2.2  rmind 
    138  1.2.2.2  rmind 	/* configure left pin */
    139  1.2.2.2  rmind 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT);
    140  1.2.2.2  rmind 	if (!(caps & GPIO_PIN_INPUT)) {
    141  1.2.2.2  rmind 		aprint_error(": pin is unable to read input\n");
    142  1.2.2.2  rmind 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    143  1.2.2.2  rmind 		return;
    144  1.2.2.2  rmind 	}
    145  1.2.2.2  rmind 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT,
    146  1.2.2.2  rmind 	    GPIO_PIN_INPUT);
    147  1.2.2.2  rmind 
    148  1.2.2.2  rmind 	/* configure right pin */
    149  1.2.2.2  rmind 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT);
    150  1.2.2.2  rmind 	if (!(caps & GPIO_PIN_INPUT)) {
    151  1.2.2.2  rmind 		aprint_error(": pin is unable to read input\n");
    152  1.2.2.2  rmind 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    153  1.2.2.2  rmind 		return;
    154  1.2.2.2  rmind 	}
    155  1.2.2.2  rmind 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT,
    156  1.2.2.2  rmind 	    GPIO_PIN_INPUT);
    157  1.2.2.2  rmind 
    158  1.2.2.2  rmind 	/* interrupt settings */
    159  1.2.2.2  rmind 	sc->sc_intr[0] = intr_establish(GPIO1_BASE + ga->ga_offset,
    160  1.2.2.2  rmind 	    IPL_VM, IST_EDGE_BOTH, mousebtn_intr, sc);
    161  1.2.2.2  rmind 	if (sc->sc_intr[0] == NULL) {
    162  1.2.2.2  rmind 		aprint_error(": couldn't establish interrupt\n");
    163  1.2.2.2  rmind 		return;
    164  1.2.2.2  rmind 	}
    165  1.2.2.2  rmind 	sc->sc_intr[1] = intr_establish(GPIO1_BASE + ga->ga_offset + 1,
    166  1.2.2.2  rmind 	     IPL_VM, IST_EDGE_BOTH, mousebtn_intr, sc);
    167  1.2.2.2  rmind 	if (sc->sc_intr[1] == NULL) {
    168  1.2.2.2  rmind 		aprint_error(": couldn't establish interrupt\n");
    169  1.2.2.2  rmind 		intr_disestablish(sc->sc_intr[0]);
    170  1.2.2.2  rmind 		return;
    171  1.2.2.2  rmind 	}
    172  1.2.2.2  rmind 
    173  1.2.2.2  rmind 	aprint_normal(": NetWalker mouse button\n");
    174  1.2.2.2  rmind 	aprint_naive(": NetWalker mouse button\n");
    175  1.2.2.2  rmind 
    176  1.2.2.2  rmind 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    177  1.2.2.2  rmind 	callout_init(&sc->sc_c, 0);
    178  1.2.2.2  rmind 
    179  1.2.2.2  rmind 	a.accessops = &mousebtn_accessops;
    180  1.2.2.2  rmind 	a.accesscookie = sc;
    181  1.2.2.2  rmind 
    182  1.2.2.2  rmind 	sc->sc_buttons = 0;
    183  1.2.2.2  rmind 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    184  1.2.2.2  rmind }
    185  1.2.2.2  rmind 
    186  1.2.2.2  rmind static int
    187  1.2.2.2  rmind mousebtn_detach(device_t self, int flags)
    188  1.2.2.2  rmind {
    189  1.2.2.2  rmind 	struct mousebtn_softc *sc = device_private(self);
    190  1.2.2.2  rmind 
    191  1.2.2.2  rmind 	if (sc->sc_intr[0] != NULL)
    192  1.2.2.2  rmind 		intr_disestablish(sc->sc_intr[0]);
    193  1.2.2.2  rmind 	if (sc->sc_intr[1] != NULL)
    194  1.2.2.2  rmind 		intr_disestablish(sc->sc_intr[1]);
    195  1.2.2.2  rmind 
    196  1.2.2.2  rmind 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    197  1.2.2.2  rmind 
    198  1.2.2.2  rmind 	return 0;
    199  1.2.2.2  rmind }
    200  1.2.2.2  rmind 
    201  1.2.2.2  rmind static void
    202  1.2.2.2  rmind mousebtn_poll(void *arg)
    203  1.2.2.2  rmind {
    204  1.2.2.2  rmind 	struct mousebtn_softc *sc = (struct mousebtn_softc *)arg;
    205  1.2.2.2  rmind 	uint32_t buttons = 0;
    206  1.2.2.2  rmind 	int s;
    207  1.2.2.2  rmind 	int left;
    208  1.2.2.2  rmind 	int right;
    209  1.2.2.2  rmind 
    210  1.2.2.2  rmind 	mutex_enter(&sc->sc_lock);
    211  1.2.2.2  rmind 
    212  1.2.2.2  rmind 	left = !gpio_pin_read(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT);
    213  1.2.2.2  rmind 	right = !gpio_pin_read(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT);
    214  1.2.2.2  rmind 	buttons = (right << 2) | left;
    215  1.2.2.2  rmind 
    216  1.2.2.2  rmind 	if (sc->sc_buttons != buttons) {
    217  1.2.2.2  rmind 		s = spltty();
    218  1.2.2.2  rmind 		wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, 0, 0,
    219  1.2.2.2  rmind 		    WSMOUSE_INPUT_DELTA);
    220  1.2.2.2  rmind 		sc->sc_buttons = buttons;
    221  1.2.2.2  rmind 		splx(s);
    222  1.2.2.2  rmind 	}
    223  1.2.2.2  rmind 
    224  1.2.2.2  rmind 	mutex_exit(&sc->sc_lock);
    225  1.2.2.2  rmind 
    226  1.2.2.2  rmind #if defined(MOUSEBTN_POLLING)
    227  1.2.2.2  rmind 	if (sc->sc_enabled)
    228  1.2.2.2  rmind 		callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
    229  1.2.2.2  rmind #endif
    230  1.2.2.2  rmind 	return;
    231  1.2.2.2  rmind }
    232  1.2.2.2  rmind 
    233  1.2.2.2  rmind static int
    234  1.2.2.2  rmind mousebtn_intr(void *v)
    235  1.2.2.2  rmind {
    236  1.2.2.2  rmind 	struct mousebtn_softc *sc = v;
    237  1.2.2.2  rmind 
    238  1.2.2.2  rmind 	if (sc->sc_enabled)
    239  1.2.2.2  rmind 		callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
    240  1.2.2.2  rmind 
    241  1.2.2.2  rmind 	return 1;
    242  1.2.2.2  rmind }
    243  1.2.2.2  rmind 
    244  1.2.2.2  rmind 
    245  1.2.2.2  rmind int
    246  1.2.2.2  rmind mousebtn_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    247  1.2.2.2  rmind {
    248  1.2.2.2  rmind 	struct wsmouse_id *id;
    249  1.2.2.2  rmind 
    250  1.2.2.2  rmind 	switch (cmd) {
    251  1.2.2.2  rmind 	case WSMOUSEIO_GTYPE:
    252  1.2.2.2  rmind 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    253  1.2.2.2  rmind 		return 0;
    254  1.2.2.2  rmind 	case WSMOUSEIO_GETID:
    255  1.2.2.2  rmind 		id = (struct wsmouse_id *)data;
    256  1.2.2.2  rmind 		if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
    257  1.2.2.2  rmind 			return EINVAL;
    258  1.2.2.2  rmind 		strlcpy(id->data, "NetWalker Mouse Button", WSMOUSE_ID_MAXLEN);
    259  1.2.2.2  rmind 		id->length = strlen(id->data);
    260  1.2.2.2  rmind 		return 0;
    261  1.2.2.2  rmind 	}
    262  1.2.2.2  rmind 
    263  1.2.2.2  rmind 	return EPASSTHROUGH;
    264  1.2.2.2  rmind }
    265  1.2.2.2  rmind 
    266  1.2.2.2  rmind int
    267  1.2.2.2  rmind mousebtn_enable(void *v)
    268  1.2.2.2  rmind {
    269  1.2.2.2  rmind 	struct mousebtn_softc *sc = (struct mousebtn_softc *)v;
    270  1.2.2.2  rmind 
    271  1.2.2.2  rmind 	if (sc->sc_enabled)
    272  1.2.2.2  rmind 		return EBUSY;
    273  1.2.2.2  rmind 
    274  1.2.2.2  rmind 	if (!pmf_device_register(sc->sc_dev, mousebtn_suspend, mousebtn_resume))
    275  1.2.2.2  rmind 		aprint_error_dev(sc->sc_dev,
    276  1.2.2.2  rmind 		    "couldn't establish power handler\n");
    277  1.2.2.2  rmind 
    278  1.2.2.2  rmind 	sc->sc_enabled = 1;
    279  1.2.2.2  rmind #if defined(MOUSEBTN_POLLING)
    280  1.2.2.2  rmind 	callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
    281  1.2.2.2  rmind #endif
    282  1.2.2.2  rmind 
    283  1.2.2.2  rmind 	return 0;
    284  1.2.2.2  rmind }
    285  1.2.2.2  rmind 
    286  1.2.2.2  rmind void
    287  1.2.2.2  rmind mousebtn_disable(void *v)
    288  1.2.2.2  rmind {
    289  1.2.2.2  rmind 	struct mousebtn_softc *sc = (struct mousebtn_softc *)v;
    290  1.2.2.2  rmind 
    291  1.2.2.2  rmind 	if (!sc->sc_enabled)
    292  1.2.2.2  rmind 		return;
    293  1.2.2.2  rmind 
    294  1.2.2.2  rmind 	pmf_device_deregister(sc->sc_dev);
    295  1.2.2.2  rmind 
    296  1.2.2.2  rmind 	sc->sc_enabled = 0;
    297  1.2.2.2  rmind 
    298  1.2.2.2  rmind 	return;
    299  1.2.2.2  rmind }
    300  1.2.2.2  rmind 
    301  1.2.2.2  rmind static bool
    302  1.2.2.2  rmind mousebtn_suspend(device_t dv, const pmf_qual_t *qual)
    303  1.2.2.2  rmind {
    304  1.2.2.2  rmind 	struct mousebtn_softc *sc = device_private(dv);
    305  1.2.2.2  rmind 
    306  1.2.2.2  rmind #if defined(MOUSEBTN_POLLING)
    307  1.2.2.2  rmind 	callout_stop(&sc->sc_c);
    308  1.2.2.2  rmind #endif
    309  1.2.2.2  rmind 	sc->sc_enabled = 0;
    310  1.2.2.2  rmind 
    311  1.2.2.2  rmind 	return true;
    312  1.2.2.2  rmind }
    313  1.2.2.2  rmind 
    314  1.2.2.2  rmind static bool
    315  1.2.2.2  rmind mousebtn_resume(device_t dv, const pmf_qual_t *qual)
    316  1.2.2.2  rmind {
    317  1.2.2.2  rmind 	struct mousebtn_softc *sc = device_private(dv);
    318  1.2.2.2  rmind 
    319  1.2.2.2  rmind 	sc->sc_enabled = 1;
    320  1.2.2.2  rmind #if defined(MOUSEBTN_POLLING)
    321  1.2.2.2  rmind 	callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
    322  1.2.2.2  rmind #endif
    323  1.2.2.2  rmind 	return true;
    324  1.2.2.2  rmind }
    325  1.2.2.2  rmind 
    326