Home | History | Annotate | Line # | Download | only in ic
spic.c revision 1.7.2.2
      1  1.7.2.2        ad /*	$NetBSD: spic.c,v 1.7.2.2 2007/10/23 20:07:46 ad 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.7.2.2        ad __KERNEL_RCSID(0, "$NetBSD: spic.c,v 1.7.2.2 2007/10/23 20:07:46 ad 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.7.2.2        ad #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.1  augustss #define POLLRATE (hz/30)
     78      1.1  augustss 
     79      1.1  augustss /* Some hardware constants */
     80      1.1  augustss #define SPIC_PORT1 0
     81      1.1  augustss #define SPIC_PORT2 4
     82      1.1  augustss 
     83      1.1  augustss #ifdef SPIC_DEBUG
     84      1.1  augustss int spicdebug = 0;
     85      1.1  augustss #endif
     86      1.1  augustss 
     87      1.3  jmcneill static int spicerror = 0;
     88      1.3  jmcneill 
     89      1.1  augustss static int	spic_enable(void *);
     90      1.1  augustss static void	spic_disable(void *);
     91      1.7  christos static int	spic_ioctl(void *, u_long, void *, int, struct lwp *);
     92      1.1  augustss 
     93      1.1  augustss static const struct wsmouse_accessops spic_accessops = {
     94      1.1  augustss 	spic_enable,
     95      1.1  augustss 	spic_ioctl,
     96      1.1  augustss 	spic_disable,
     97      1.1  augustss };
     98      1.1  augustss 
     99      1.1  augustss #define SPIC_COMMAND(quiet, command) do { \
    100      1.1  augustss 	unsigned int n = 10000; \
    101      1.1  augustss 	while (--n && (command)) \
    102      1.1  augustss 		delay(1); \
    103      1.3  jmcneill 	if (n == 0 && !(quiet)) { \
    104      1.3  jmcneill 		printf("spic0: command failed at line %d\n", __LINE__); \
    105      1.3  jmcneill 		spicerror++; \
    106      1.3  jmcneill 	} \
    107      1.1  augustss } while (0)
    108      1.1  augustss 
    109      1.1  augustss #if 0
    110      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)))
    111      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)
    112      1.1  augustss #else
    113      1.1  augustss #define INB(sc, 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)); } while(0)
    115      1.1  augustss #endif
    116      1.1  augustss 
    117      1.1  augustss static u_int8_t
    118      1.1  augustss spic_call1(struct spic_softc *sc, u_int8_t dev)
    119      1.1  augustss {
    120      1.1  augustss 	u_int8_t v1, v2;
    121      1.1  augustss 
    122      1.1  augustss 	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
    123      1.1  augustss 	OUTB(sc, dev, SPIC_PORT2);
    124      1.1  augustss 	v1 = INB(sc, SPIC_PORT2);
    125      1.1  augustss 	v2 = INB(sc, SPIC_PORT1);
    126      1.1  augustss 	return v2;
    127      1.1  augustss }
    128      1.1  augustss 
    129      1.1  augustss static u_int8_t
    130      1.1  augustss spic_call2(struct spic_softc *sc, u_int8_t dev, u_int8_t fn)
    131      1.1  augustss {
    132      1.1  augustss 	u_int8_t v1;
    133      1.1  augustss 
    134      1.1  augustss 	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
    135      1.1  augustss 	OUTB(sc, dev, SPIC_PORT2);
    136      1.1  augustss 	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
    137      1.1  augustss 	OUTB(sc, fn, SPIC_PORT1);
    138      1.1  augustss 	v1 = INB(sc, SPIC_PORT1);
    139      1.1  augustss 	return v1;
    140      1.1  augustss }
    141      1.1  augustss 
    142      1.1  augustss /* Interrupt handler: some event is available */
    143      1.1  augustss int
    144      1.1  augustss spic_intr(void *v) {
    145      1.1  augustss 	struct spic_softc *sc = v;
    146      1.1  augustss 	u_int8_t v1, v2;
    147      1.1  augustss 	int dz, buttons;
    148      1.1  augustss 
    149      1.1  augustss 	v1 = INB(sc, SPIC_PORT1);
    150      1.1  augustss 	v2 = INB(sc, SPIC_PORT2);
    151      1.1  augustss 
    152      1.3  jmcneill 	/* Handle lid switch */
    153      1.3  jmcneill 	if (v2 == 0x30) {
    154      1.3  jmcneill 		switch (v1) {
    155      1.3  jmcneill 		case 0x50:	/* opened */
    156      1.3  jmcneill 			sysmon_pswitch_event(&sc->sc_smpsw[SPIC_PSWITCH_LID],
    157      1.3  jmcneill 			    PSWITCH_EVENT_RELEASED);
    158      1.3  jmcneill 			goto skip;
    159      1.3  jmcneill 			break;
    160      1.3  jmcneill 		case 0x51:	/* closed */
    161      1.3  jmcneill 			sysmon_pswitch_event(&sc->sc_smpsw[SPIC_PSWITCH_LID],
    162      1.3  jmcneill 			    PSWITCH_EVENT_PRESSED);
    163      1.3  jmcneill 			goto skip;
    164      1.3  jmcneill 			break;
    165      1.3  jmcneill 		default:
    166      1.3  jmcneill 			aprint_debug("%s: unknown lid event 0x%02x\n",
    167      1.3  jmcneill 			    sc->sc_dev.dv_xname, v1);
    168      1.3  jmcneill 			goto skip;
    169      1.3  jmcneill 			break;
    170      1.3  jmcneill 		}
    171      1.3  jmcneill 	}
    172      1.3  jmcneill 
    173      1.3  jmcneill 	/* Handle suspend/hibernate buttons */
    174      1.3  jmcneill 	if (v2 == 0x20) {
    175      1.3  jmcneill 		switch (v1) {
    176      1.3  jmcneill 		case 0x10:	/* suspend */
    177      1.3  jmcneill 			sysmon_pswitch_event(
    178      1.3  jmcneill 			    &sc->sc_smpsw[SPIC_PSWITCH_SUSPEND],
    179      1.3  jmcneill 			    PSWITCH_EVENT_PRESSED);
    180      1.3  jmcneill 			goto skip;
    181      1.3  jmcneill 			break;
    182      1.3  jmcneill 		case 0x1c:	/* hibernate */
    183      1.3  jmcneill 			sysmon_pswitch_event(
    184      1.3  jmcneill 			    &sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE],
    185      1.3  jmcneill 			    PSWITCH_EVENT_PRESSED);
    186      1.3  jmcneill 			goto skip;
    187      1.3  jmcneill 			break;
    188      1.3  jmcneill 		}
    189      1.3  jmcneill 	}
    190      1.3  jmcneill 
    191      1.1  augustss 	buttons = 0;
    192      1.1  augustss 	if (v1 & 0x40)
    193      1.1  augustss 		buttons |= 1 << 1;
    194      1.1  augustss 	if (v1 & 0x20)
    195      1.1  augustss 		buttons |= 1 << 5;
    196      1.1  augustss 	dz = v1 & 0x1f;
    197      1.1  augustss 	switch (dz) {
    198      1.1  augustss 	case 0:
    199      1.1  augustss 	case 1:
    200      1.1  augustss 	case 2:
    201      1.1  augustss 	case 3:
    202      1.1  augustss 		break;
    203      1.1  augustss 	case 0x1f:
    204      1.1  augustss 	case 0x1e:
    205      1.1  augustss 	case 0x1d:
    206      1.1  augustss 		dz -= 0x20;
    207      1.1  augustss 		break;
    208      1.1  augustss 	default:
    209      1.3  jmcneill 		printf("spic0: v1=0x%02x v2=0x%02x\n", v1, v2);
    210      1.1  augustss 		goto skip;
    211      1.1  augustss 	}
    212      1.1  augustss 
    213      1.1  augustss 	if (!sc->sc_enabled) {
    214      1.1  augustss 		/*printf("spic: not enabled\n");*/
    215      1.1  augustss 		goto skip;
    216      1.1  augustss 	}
    217      1.1  augustss 
    218      1.1  augustss 	if (dz != 0 || buttons != sc->sc_buttons) {
    219      1.1  augustss #ifdef SPIC_DEBUG
    220      1.1  augustss 		if (spicdebug)
    221      1.1  augustss 			printf("spic: but=0x%x dz=%d v1=0x%02x v2=0x%02x\n",
    222      1.1  augustss 			       buttons, dz, v1, v2);
    223      1.1  augustss #endif
    224      1.1  augustss 		sc->sc_buttons = buttons;
    225      1.1  augustss 		if (sc->sc_wsmousedev != NULL) {
    226      1.5    plunky 			wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, dz, 0,
    227      1.1  augustss 				      WSMOUSE_INPUT_DELTA);
    228      1.1  augustss 		}
    229      1.1  augustss 	}
    230      1.1  augustss 
    231      1.1  augustss skip:
    232      1.1  augustss 	spic_call2(sc, 0x81, 0xff); /* Clear event */
    233      1.1  augustss 	return (1);
    234      1.1  augustss }
    235      1.1  augustss 
    236      1.1  augustss static void
    237      1.1  augustss spictimeout(void *v)
    238      1.1  augustss {
    239      1.1  augustss 	struct spic_softc *sc = v;
    240      1.3  jmcneill 	int s;
    241      1.3  jmcneill 
    242      1.3  jmcneill 	if (spicerror >= 3)
    243      1.3  jmcneill 		return;
    244      1.3  jmcneill 
    245      1.3  jmcneill 	s = spltty();
    246      1.1  augustss 	spic_intr(v);
    247      1.1  augustss 	splx(s);
    248      1.1  augustss 	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
    249      1.1  augustss }
    250      1.1  augustss 
    251      1.1  augustss void
    252      1.1  augustss spic_attach(struct spic_softc *sc)
    253      1.1  augustss {
    254      1.1  augustss 	struct wsmousedev_attach_args a;
    255      1.3  jmcneill 	int i, rv;
    256      1.1  augustss 
    257      1.1  augustss #ifdef SPIC_DEBUG
    258      1.1  augustss 	if (spicdebug)
    259      1.1  augustss 		printf("spic_attach %x %x\n", sc->sc_iot, (uint)sc->sc_ioh);
    260      1.1  augustss #endif
    261      1.1  augustss 
    262  1.7.2.1        ad 	callout_init(&sc->sc_poll, 0);
    263      1.1  augustss 
    264      1.1  augustss 	spic_call1(sc, 0x82);
    265      1.1  augustss 	spic_call2(sc, 0x81, 0xff);
    266      1.1  augustss 	spic_call1(sc, 0x92);	/* or 0x82 */
    267      1.1  augustss 
    268      1.1  augustss 	a.accessops = &spic_accessops;
    269      1.1  augustss 	a.accesscookie = sc;
    270      1.1  augustss 	sc->sc_wsmousedev = config_found(&sc->sc_dev, &a, wsmousedevprint);
    271      1.3  jmcneill 
    272      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_LID].smpsw_name = "spiclid0";
    273      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_LID].smpsw_type = PSWITCH_TYPE_LID;
    274      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_SUSPEND].smpsw_name = "spicsuspend0";
    275      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_SUSPEND].smpsw_type = PSWITCH_TYPE_SLEEP;
    276      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE].smpsw_name = "spichibernate0";
    277      1.3  jmcneill 	sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE].smpsw_type = PSWITCH_TYPE_SLEEP;
    278      1.3  jmcneill 
    279      1.3  jmcneill 	for (i = 0; i < SPIC_NPSWITCH; i++) {
    280      1.3  jmcneill 		rv = sysmon_pswitch_register(&sc->sc_smpsw[i]);
    281      1.3  jmcneill 		if (rv != 0)
    282      1.3  jmcneill 			aprint_error("%s: unable to register %s with sysmon\n",
    283      1.3  jmcneill 			    sc->sc_dev.dv_xname,
    284      1.3  jmcneill 			    sc->sc_smpsw[i].smpsw_name);
    285      1.3  jmcneill 	}
    286      1.3  jmcneill 
    287      1.3  jmcneill 	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
    288      1.3  jmcneill 
    289      1.3  jmcneill 	return;
    290      1.1  augustss }
    291      1.1  augustss 
    292      1.1  augustss 
    293      1.1  augustss static int
    294      1.1  augustss spic_enable(void *v)
    295      1.1  augustss {
    296      1.1  augustss 	struct spic_softc *sc = v;
    297      1.1  augustss 
    298      1.1  augustss 	if (sc->sc_enabled)
    299      1.1  augustss 		return (EBUSY);
    300      1.1  augustss 
    301      1.1  augustss 	sc->sc_enabled = 1;
    302      1.1  augustss 	sc->sc_buttons = 0;
    303      1.1  augustss 
    304      1.1  augustss #ifdef SPIC_DEBUG
    305      1.1  augustss 	if (spicdebug)
    306      1.1  augustss 		printf("spic_enable\n");
    307      1.1  augustss #endif
    308      1.1  augustss 
    309      1.1  augustss 	return (0);
    310      1.1  augustss }
    311      1.1  augustss 
    312      1.1  augustss static void
    313      1.1  augustss spic_disable(void *v)
    314      1.1  augustss {
    315      1.1  augustss 	struct spic_softc *sc = v;
    316      1.1  augustss 
    317      1.1  augustss #ifdef DIAGNOSTIC
    318      1.1  augustss 	if (!sc->sc_enabled) {
    319      1.1  augustss 		printf("spic_disable: not enabled\n");
    320      1.1  augustss 		return;
    321      1.1  augustss 	}
    322      1.1  augustss #endif
    323      1.1  augustss 
    324      1.1  augustss 	sc->sc_enabled = 0;
    325      1.1  augustss 
    326      1.1  augustss #ifdef SPIC_DEBUG
    327      1.1  augustss 	if (spicdebug)
    328      1.1  augustss 		printf("spic_disable\n");
    329      1.1  augustss #endif
    330      1.1  augustss }
    331      1.1  augustss 
    332      1.1  augustss static int
    333      1.7  christos spic_ioctl(void *v, u_long cmd, void *data,
    334      1.6  christos     int flag, struct lwp *l)
    335      1.1  augustss {
    336      1.1  augustss 	switch (cmd) {
    337      1.1  augustss 	case WSMOUSEIO_GTYPE:
    338      1.1  augustss 		/* XXX this is not really correct */
    339      1.1  augustss 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    340      1.1  augustss 		return (0);
    341      1.1  augustss 	}
    342      1.1  augustss 
    343      1.1  augustss 	return (-1);
    344      1.1  augustss }
    345