Home | History | Annotate | Line # | Download | only in ic
spic.c revision 1.8.6.2
      1  1.8.6.2     joerg /*	$NetBSD: spic.c,v 1.8.6.2 2007/10/26 15:45:02 joerg Exp $	*/
      2      1.1  augustss 
      3      1.1  augustss /*
      4      1.1  augustss  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5      1.1  augustss  * All rights reserved.
      6      1.1  augustss  *
      7      1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9      1.1  augustss  * Carlstedt Research & Technology.
     10      1.1  augustss  *
     11      1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12      1.1  augustss  * modification, are permitted provided that the following conditions
     13      1.1  augustss  * are met:
     14      1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15      1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16      1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18      1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19      1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20      1.1  augustss  *    must display the following acknowledgement:
     21      1.1  augustss  *        This product includes software developed by the NetBSD
     22      1.1  augustss  *        Foundation, Inc. and its contributors.
     23      1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.1  augustss  *    contributors may be used to endorse or promote products derived
     25      1.1  augustss  *    from this software without specific prior written permission.
     26      1.1  augustss  *
     27      1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38      1.1  augustss  */
     39      1.1  augustss 
     40      1.1  augustss /*
     41      1.1  augustss  * The SPIC is used on some Sony Vaios to handle the jog dial and other
     42      1.1  augustss  * peripherals.
     43      1.1  augustss  * The protocol used by the SPIC seems to vary wildly among the different
     44      1.1  augustss  * models, and I've found no documentation.
     45      1.1  augustss  * This file handles the jog dial on the SRX77 model, and perhaps nothing
     46      1.1  augustss  * else.
     47      1.1  augustss  *
     48      1.1  augustss  * The general way of talking to the SPIC was gleaned from the Linux and
     49      1.1  augustss  * FreeBSD drivers.  The hex numbers were taken from these drivers (they
     50      1.1  augustss  * come from reverese engineering.)
     51      1.1  augustss  *
     52      1.1  augustss  * TODO:
     53      1.1  augustss  *   Make it handle more models.
     54      1.1  augustss  *   Figure out why the interrupt mode doesn't work.
     55      1.1  augustss  */
     56      1.1  augustss 
     57      1.1  augustss 
     58      1.1  augustss #include <sys/cdefs.h>
     59  1.8.6.2     joerg __KERNEL_RCSID(0, "$NetBSD: spic.c,v 1.8.6.2 2007/10/26 15:45:02 joerg Exp $");
     60      1.1  augustss 
     61      1.1  augustss #include <sys/param.h>
     62      1.1  augustss #include <sys/systm.h>
     63      1.1  augustss #include <sys/device.h>
     64      1.1  augustss #include <sys/proc.h>
     65      1.1  augustss #include <sys/kernel.h>
     66      1.1  augustss #include <sys/callout.h>
     67      1.1  augustss 
     68  1.8.6.2     joerg #include <sys/bus.h>
     69      1.1  augustss 
     70      1.3  jmcneill #include <dev/sysmon/sysmonvar.h>
     71      1.3  jmcneill 
     72      1.1  augustss #include <dev/ic/spicvar.h>
     73      1.1  augustss 
     74      1.1  augustss #include <dev/wscons/wsconsio.h>
     75      1.1  augustss #include <dev/wscons/wsmousevar.h>
     76      1.1  augustss 
     77  1.8.6.1  christos #define	SPIC_EVENT_BRIGHTNESS_DOWN	0x15
     78  1.8.6.1  christos #define	SPIC_EVENT_BRIGHTNESS_UP	0x16
     79  1.8.6.1  christos 
     80      1.1  augustss #define POLLRATE (hz/30)
     81      1.1  augustss 
     82      1.1  augustss /* Some hardware constants */
     83      1.1  augustss #define SPIC_PORT1 0
     84      1.1  augustss #define SPIC_PORT2 4
     85      1.1  augustss 
     86      1.1  augustss #ifdef SPIC_DEBUG
     87      1.1  augustss int spicdebug = 0;
     88      1.1  augustss #endif
     89      1.1  augustss 
     90      1.3  jmcneill static int spicerror = 0;
     91      1.3  jmcneill 
     92      1.1  augustss static int	spic_enable(void *);
     93      1.1  augustss static void	spic_disable(void *);
     94      1.7  christos static int	spic_ioctl(void *, u_long, void *, int, struct lwp *);
     95      1.1  augustss 
     96      1.1  augustss static const struct wsmouse_accessops spic_accessops = {
     97      1.1  augustss 	spic_enable,
     98      1.1  augustss 	spic_ioctl,
     99      1.1  augustss 	spic_disable,
    100      1.1  augustss };
    101      1.1  augustss 
    102      1.1  augustss #define SPIC_COMMAND(quiet, command) do { \
    103      1.1  augustss 	unsigned int n = 10000; \
    104      1.1  augustss 	while (--n && (command)) \
    105      1.1  augustss 		delay(1); \
    106      1.3  jmcneill 	if (n == 0 && !(quiet)) { \
    107      1.3  jmcneill 		printf("spic0: command failed at line %d\n", __LINE__); \
    108      1.3  jmcneill 		spicerror++; \
    109      1.3  jmcneill 	} \
    110      1.1  augustss } while (0)
    111      1.1  augustss 
    112      1.1  augustss #if 0
    113      1.1  augustss #define INB(sc, p) (delay(100), printf("inb(%x)=%x\n", (uint)sc->sc_ioh+p, bus_space_read_1(sc->sc_iot, sc->sc_ioh, p)), delay(100), bus_space_read_1(sc->sc_iot, sc->sc_ioh, (p)))
    114      1.1  augustss #define OUTB(sc, v, p) do { delay(100); bus_space_write_1(sc->sc_iot, sc->sc_ioh, (p), (v)); printf("outb(%x, %x)\n", (uint)sc->sc_ioh+p, v); } while(0)
    115      1.1  augustss #else
    116      1.1  augustss #define INB(sc, p) (delay(100), bus_space_read_1(sc->sc_iot, sc->sc_ioh, (p)))
    117      1.1  augustss #define OUTB(sc, v, p) do { delay(100); bus_space_write_1(sc->sc_iot, sc->sc_ioh, (p), (v)); } while(0)
    118      1.1  augustss #endif
    119      1.1  augustss 
    120      1.1  augustss static u_int8_t
    121      1.1  augustss spic_call1(struct spic_softc *sc, u_int8_t dev)
    122      1.1  augustss {
    123      1.1  augustss 	u_int8_t v1, v2;
    124      1.1  augustss 
    125      1.1  augustss 	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
    126      1.1  augustss 	OUTB(sc, dev, SPIC_PORT2);
    127      1.1  augustss 	v1 = INB(sc, SPIC_PORT2);
    128      1.1  augustss 	v2 = INB(sc, SPIC_PORT1);
    129      1.1  augustss 	return v2;
    130      1.1  augustss }
    131      1.1  augustss 
    132      1.1  augustss static u_int8_t
    133      1.1  augustss spic_call2(struct spic_softc *sc, u_int8_t dev, u_int8_t fn)
    134      1.1  augustss {
    135      1.1  augustss 	u_int8_t v1;
    136      1.1  augustss 
    137      1.1  augustss 	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
    138      1.1  augustss 	OUTB(sc, dev, SPIC_PORT2);
    139      1.1  augustss 	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
    140      1.1  augustss 	OUTB(sc, fn, SPIC_PORT1);
    141      1.1  augustss 	v1 = INB(sc, SPIC_PORT1);
    142      1.1  augustss 	return v1;
    143      1.1  augustss }
    144      1.1  augustss 
    145      1.1  augustss /* Interrupt handler: some event is available */
    146      1.1  augustss int
    147      1.1  augustss spic_intr(void *v) {
    148      1.1  augustss 	struct spic_softc *sc = v;
    149      1.1  augustss 	u_int8_t v1, v2;
    150      1.1  augustss 	int dz, buttons;
    151      1.1  augustss 
    152      1.1  augustss 	v1 = INB(sc, SPIC_PORT1);
    153      1.1  augustss 	v2 = INB(sc, SPIC_PORT2);
    154      1.1  augustss 
    155      1.3  jmcneill 	/* Handle lid switch */
    156      1.3  jmcneill 	if (v2 == 0x30) {
    157      1.3  jmcneill 		switch (v1) {
    158      1.3  jmcneill 		case 0x50:	/* opened */
    159      1.3  jmcneill 			sysmon_pswitch_event(&sc->sc_smpsw[SPIC_PSWITCH_LID],
    160      1.3  jmcneill 			    PSWITCH_EVENT_RELEASED);
    161      1.3  jmcneill 			goto skip;
    162      1.3  jmcneill 			break;
    163      1.3  jmcneill 		case 0x51:	/* closed */
    164      1.3  jmcneill 			sysmon_pswitch_event(&sc->sc_smpsw[SPIC_PSWITCH_LID],
    165      1.3  jmcneill 			    PSWITCH_EVENT_PRESSED);
    166      1.3  jmcneill 			goto skip;
    167      1.3  jmcneill 			break;
    168      1.3  jmcneill 		default:
    169      1.3  jmcneill 			aprint_debug("%s: unknown lid event 0x%02x\n",
    170      1.3  jmcneill 			    sc->sc_dev.dv_xname, v1);
    171      1.3  jmcneill 			goto skip;
    172      1.3  jmcneill 			break;
    173      1.3  jmcneill 		}
    174      1.3  jmcneill 	}
    175      1.3  jmcneill 
    176      1.3  jmcneill 	/* Handle suspend/hibernate buttons */
    177      1.3  jmcneill 	if (v2 == 0x20) {
    178      1.3  jmcneill 		switch (v1) {
    179      1.3  jmcneill 		case 0x10:	/* suspend */
    180      1.3  jmcneill 			sysmon_pswitch_event(
    181      1.3  jmcneill 			    &sc->sc_smpsw[SPIC_PSWITCH_SUSPEND],
    182      1.3  jmcneill 			    PSWITCH_EVENT_PRESSED);
    183      1.3  jmcneill 			goto skip;
    184      1.3  jmcneill 			break;
    185      1.3  jmcneill 		case 0x1c:	/* hibernate */
    186      1.3  jmcneill 			sysmon_pswitch_event(
    187      1.3  jmcneill 			    &sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE],
    188      1.3  jmcneill 			    PSWITCH_EVENT_PRESSED);
    189      1.3  jmcneill 			goto skip;
    190      1.3  jmcneill 			break;
    191      1.3  jmcneill 		}
    192      1.3  jmcneill 	}
    193      1.3  jmcneill 
    194      1.1  augustss 	buttons = 0;
    195      1.1  augustss 	if (v1 & 0x40)
    196      1.1  augustss 		buttons |= 1 << 1;
    197      1.1  augustss 	if (v1 & 0x20)
    198      1.1  augustss 		buttons |= 1 << 5;
    199      1.1  augustss 	dz = v1 & 0x1f;
    200      1.1  augustss 	switch (dz) {
    201      1.1  augustss 	case 0:
    202      1.1  augustss 	case 1:
    203      1.1  augustss 	case 2:
    204      1.1  augustss 	case 3:
    205      1.1  augustss 		break;
    206      1.1  augustss 	case 0x1f:
    207      1.1  augustss 	case 0x1e:
    208      1.1  augustss 	case 0x1d:
    209      1.1  augustss 		dz -= 0x20;
    210      1.1  augustss 		break;
    211  1.8.6.1  christos 	case SPIC_EVENT_BRIGHTNESS_UP:
    212  1.8.6.1  christos 		pnp_power_input(NULL, PNP_ACTION_BRIGHTNESS_UP);
    213  1.8.6.1  christos 		break;
    214  1.8.6.1  christos 	case SPIC_EVENT_BRIGHTNESS_DOWN:
    215  1.8.6.1  christos 		pnp_power_input(NULL, PNP_ACTION_BRIGHTNESS_DOWN);
    216  1.8.6.1  christos 		break;
    217      1.1  augustss 	default:
    218      1.3  jmcneill 		printf("spic0: v1=0x%02x v2=0x%02x\n", v1, v2);
    219      1.1  augustss 		goto skip;
    220      1.1  augustss 	}
    221      1.1  augustss 
    222      1.1  augustss 	if (!sc->sc_enabled) {
    223      1.1  augustss 		/*printf("spic: not enabled\n");*/
    224      1.1  augustss 		goto skip;
    225      1.1  augustss 	}
    226      1.1  augustss 
    227      1.1  augustss 	if (dz != 0 || buttons != sc->sc_buttons) {
    228      1.1  augustss #ifdef SPIC_DEBUG
    229      1.1  augustss 		if (spicdebug)
    230      1.1  augustss 			printf("spic: but=0x%x dz=%d v1=0x%02x v2=0x%02x\n",
    231      1.1  augustss 			       buttons, dz, v1, v2);
    232      1.1  augustss #endif
    233      1.1  augustss 		sc->sc_buttons = buttons;
    234      1.1  augustss 		if (sc->sc_wsmousedev != NULL) {
    235      1.5    plunky 			wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, dz, 0,
    236      1.1  augustss 				      WSMOUSE_INPUT_DELTA);
    237      1.1  augustss 		}
    238      1.1  augustss 	}
    239      1.1  augustss 
    240      1.1  augustss skip:
    241      1.1  augustss 	spic_call2(sc, 0x81, 0xff); /* Clear event */
    242      1.1  augustss 	return (1);
    243      1.1  augustss }
    244      1.1  augustss 
    245      1.1  augustss static void
    246      1.1  augustss spictimeout(void *v)
    247      1.1  augustss {
    248      1.1  augustss 	struct spic_softc *sc = v;
    249      1.3  jmcneill 	int s;
    250      1.3  jmcneill 
    251      1.3  jmcneill 	if (spicerror >= 3)
    252      1.3  jmcneill 		return;
    253      1.3  jmcneill 
    254      1.3  jmcneill 	s = spltty();
    255      1.1  augustss 	spic_intr(v);
    256      1.1  augustss 	splx(s);
    257      1.1  augustss 	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
    258      1.1  augustss }
    259      1.1  augustss 
    260      1.1  augustss void
    261      1.1  augustss spic_attach(struct spic_softc *sc)
    262      1.1  augustss {
    263      1.1  augustss 	struct wsmousedev_attach_args a;
    264      1.3  jmcneill 	int i, rv;
    265      1.1  augustss 
    266      1.1  augustss #ifdef SPIC_DEBUG
    267      1.1  augustss 	if (spicdebug)
    268      1.1  augustss 		printf("spic_attach %x %x\n", sc->sc_iot, (uint)sc->sc_ioh);
    269      1.1  augustss #endif
    270      1.1  augustss 
    271      1.8        ad 	callout_init(&sc->sc_poll, 0);
    272      1.1  augustss 
    273      1.1  augustss 	spic_call1(sc, 0x82);
    274      1.1  augustss 	spic_call2(sc, 0x81, 0xff);
    275      1.1  augustss 	spic_call1(sc, 0x92);	/* or 0x82 */
    276      1.1  augustss 
    277      1.1  augustss 	a.accessops = &spic_accessops;
    278      1.1  augustss 	a.accesscookie = sc;
    279      1.1  augustss 	sc->sc_wsmousedev = config_found(&sc->sc_dev, &a, wsmousedevprint);
    280      1.3  jmcneill 
    281      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_LID].smpsw_name = "spiclid0";
    282      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_LID].smpsw_type = PSWITCH_TYPE_LID;
    283      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_SUSPEND].smpsw_name = "spicsuspend0";
    284      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_SUSPEND].smpsw_type = PSWITCH_TYPE_SLEEP;
    285      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE].smpsw_name = "spichibernate0";
    286      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE].smpsw_type = PSWITCH_TYPE_SLEEP;
    287      1.3  jmcneill 
    288      1.3  jmcneill 	for (i = 0; i < SPIC_NPSWITCH; i++) {
    289      1.3  jmcneill 		rv = sysmon_pswitch_register(&sc->sc_smpsw[i]);
    290      1.3  jmcneill 		if (rv != 0)
    291      1.3  jmcneill 			aprint_error("%s: unable to register %s with sysmon\n",
    292      1.3  jmcneill 			    sc->sc_dev.dv_xname,
    293      1.3  jmcneill 			    sc->sc_smpsw[i].smpsw_name);
    294      1.3  jmcneill 	}
    295      1.3  jmcneill 
    296      1.3  jmcneill 	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
    297      1.3  jmcneill 
    298      1.3  jmcneill 	return;
    299      1.1  augustss }
    300      1.1  augustss 
    301      1.1  augustss 
    302      1.1  augustss static int
    303      1.1  augustss spic_enable(void *v)
    304      1.1  augustss {
    305      1.1  augustss 	struct spic_softc *sc = v;
    306      1.1  augustss 
    307      1.1  augustss 	if (sc->sc_enabled)
    308      1.1  augustss 		return (EBUSY);
    309      1.1  augustss 
    310      1.1  augustss 	sc->sc_enabled = 1;
    311      1.1  augustss 	sc->sc_buttons = 0;
    312      1.1  augustss 
    313      1.1  augustss #ifdef SPIC_DEBUG
    314      1.1  augustss 	if (spicdebug)
    315      1.1  augustss 		printf("spic_enable\n");
    316      1.1  augustss #endif
    317      1.1  augustss 
    318      1.1  augustss 	return (0);
    319      1.1  augustss }
    320      1.1  augustss 
    321      1.1  augustss static void
    322      1.1  augustss spic_disable(void *v)
    323      1.1  augustss {
    324      1.1  augustss 	struct spic_softc *sc = v;
    325      1.1  augustss 
    326      1.1  augustss #ifdef DIAGNOSTIC
    327      1.1  augustss 	if (!sc->sc_enabled) {
    328      1.1  augustss 		printf("spic_disable: not enabled\n");
    329      1.1  augustss 		return;
    330      1.1  augustss 	}
    331      1.1  augustss #endif
    332      1.1  augustss 
    333      1.1  augustss 	sc->sc_enabled = 0;
    334      1.1  augustss 
    335      1.1  augustss #ifdef SPIC_DEBUG
    336      1.1  augustss 	if (spicdebug)
    337      1.1  augustss 		printf("spic_disable\n");
    338      1.1  augustss #endif
    339      1.1  augustss }
    340      1.1  augustss 
    341      1.1  augustss static int
    342      1.7  christos spic_ioctl(void *v, u_long cmd, void *data,
    343      1.6  christos     int flag, struct lwp *l)
    344      1.1  augustss {
    345      1.1  augustss 	switch (cmd) {
    346      1.1  augustss 	case WSMOUSEIO_GTYPE:
    347      1.1  augustss 		/* XXX this is not really correct */
    348      1.1  augustss 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    349      1.1  augustss 		return (0);
    350      1.1  augustss 	}
    351      1.1  augustss 
    352      1.1  augustss 	return (-1);
    353      1.1  augustss }
    354