Home | History | Annotate | Line # | Download | only in dev
tctrl.c revision 1.40
      1  1.40   xtraeme /*	$NetBSD: tctrl.c,v 1.40 2007/07/01 20:01:44 xtraeme Exp $	*/
      2   1.1      matt 
      3   1.1      matt /*-
      4  1.30  macallan  * Copyright (c) 1998, 2005, 2006 The NetBSD Foundation, Inc.
      5   1.1      matt  * All rights reserved.
      6   1.1      matt  *
      7   1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      matt  * by Matt Thomas.
      9   1.1      matt  *
     10   1.1      matt  * Redistribution and use in source and binary forms, with or without
     11   1.1      matt  * modification, are permitted provided that the following conditions
     12   1.1      matt  * are met:
     13   1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14   1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15   1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      matt  *    documentation and/or other materials provided with the distribution.
     18   1.1      matt  * 3. All advertising materials mentioning features or use of this software
     19   1.1      matt  *    must display the following acknowledgement:
     20   1.1      matt  *        This product includes software developed by the NetBSD
     21   1.1      matt  *        Foundation, Inc. and its contributors.
     22   1.1      matt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      matt  *    contributors may be used to endorse or promote products derived
     24   1.1      matt  *    from this software without specific prior written permission.
     25   1.1      matt  *
     26   1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      matt  */
     38  1.25     lukem 
     39  1.25     lukem #include <sys/cdefs.h>
     40  1.40   xtraeme __KERNEL_RCSID(0, "$NetBSD: tctrl.c,v 1.40 2007/07/01 20:01:44 xtraeme Exp $");
     41   1.1      matt 
     42   1.1      matt #include <sys/param.h>
     43   1.1      matt #include <sys/systm.h>
     44   1.1      matt #include <sys/ioctl.h>
     45   1.1      matt #include <sys/select.h>
     46   1.1      matt #include <sys/tty.h>
     47   1.1      matt #include <sys/proc.h>
     48   1.1      matt #include <sys/user.h>
     49   1.1      matt #include <sys/conf.h>
     50   1.1      matt #include <sys/file.h>
     51   1.1      matt #include <sys/uio.h>
     52   1.1      matt #include <sys/kernel.h>
     53  1.30  macallan #include <sys/kthread.h>
     54   1.1      matt #include <sys/syslog.h>
     55   1.1      matt #include <sys/types.h>
     56   1.1      matt #include <sys/device.h>
     57   1.4   garbled #include <sys/envsys.h>
     58   1.4   garbled #include <sys/poll.h>
     59  1.34      elad #include <sys/kauth.h>
     60   1.1      matt 
     61   1.4   garbled #include <machine/apmvar.h>
     62   1.1      matt #include <machine/autoconf.h>
     63   1.1      matt #include <machine/bus.h>
     64  1.11        pk #include <machine/intr.h>
     65   1.4   garbled #include <machine/tctrl.h>
     66   1.1      matt 
     67   1.1      matt #include <sparc/dev/ts102reg.h>
     68   1.1      matt #include <sparc/dev/tctrlvar.h>
     69   1.7       jdc #include <sparc/sparc/auxiotwo.h>
     70  1.27  macallan #include <sparc/sparc/auxreg.h>
     71  1.27  macallan 
     72  1.27  macallan #include <dev/sysmon/sysmonvar.h>
     73  1.27  macallan #include <dev/sysmon/sysmon_taskq.h>
     74  1.30  macallan 
     75  1.27  macallan #include "sysmon_envsys.h"
     76   1.1      matt 
     77  1.30  macallan /*#define TCTRLDEBUG*/
     78  1.30  macallan 
     79  1.30  macallan /* disk spinner */
     80  1.30  macallan #include <sys/disk.h>
     81  1.30  macallan #include <dev/scsipi/sdvar.h>
     82  1.30  macallan 
     83  1.30  macallan /* ethernet carrier */
     84  1.30  macallan #include <net/if.h>
     85  1.30  macallan #include <net/if_dl.h>
     86  1.30  macallan #include <net/if_ether.h>
     87  1.30  macallan #include <net/if_media.h>
     88  1.30  macallan #include <dev/ic/lancevar.h>
     89  1.30  macallan 
     90  1.15   gehenna extern struct cfdriver tctrl_cd;
     91   1.4   garbled 
     92  1.15   gehenna dev_type_open(tctrlopen);
     93  1.15   gehenna dev_type_close(tctrlclose);
     94  1.15   gehenna dev_type_ioctl(tctrlioctl);
     95  1.15   gehenna dev_type_poll(tctrlpoll);
     96  1.20  jdolecek dev_type_kqfilter(tctrlkqfilter);
     97  1.15   gehenna 
     98  1.15   gehenna const struct cdevsw tctrl_cdevsw = {
     99  1.15   gehenna 	tctrlopen, tctrlclose, noread, nowrite, tctrlioctl,
    100  1.20  jdolecek 	nostop, notty, tctrlpoll, nommap, tctrlkqfilter,
    101  1.15   gehenna };
    102   1.4   garbled 
    103   1.1      matt static const char *tctrl_ext_statuses[16] = {
    104   1.1      matt 	"main power available",
    105   1.1      matt 	"internal battery attached",
    106   1.1      matt 	"external battery attached",
    107   1.1      matt 	"external VGA attached",
    108   1.1      matt 	"external keyboard attached",
    109   1.1      matt 	"external mouse attached",
    110   1.1      matt 	"lid down",
    111   1.1      matt 	"internal battery charging",
    112   1.1      matt 	"external battery charging",
    113   1.1      matt 	"internal battery discharging",
    114   1.1      matt 	"external battery discharging",
    115   1.1      matt };
    116   1.1      matt 
    117   1.1      matt struct tctrl_softc {
    118   1.4   garbled 	struct	device sc_dev;
    119   1.4   garbled 	bus_space_tag_t	sc_memt;
    120   1.4   garbled 	bus_space_handle_t	sc_memh;
    121   1.4   garbled 	unsigned int	sc_junk;
    122   1.4   garbled 	unsigned int	sc_ext_status;
    123   1.4   garbled 	unsigned int	sc_flags;
    124   1.4   garbled #define TCTRL_SEND_REQUEST		0x0001
    125   1.4   garbled #define TCTRL_APM_CTLOPEN		0x0002
    126  1.30  macallan 	uint32_t	sc_wantdata;
    127  1.30  macallan 	uint32_t	sc_ext_pending;
    128  1.30  macallan 	volatile uint16_t	sc_lcdstate;
    129  1.30  macallan 	uint16_t	sc_lcdwanted;
    130  1.30  macallan 
    131   1.1      matt 	enum { TCTRL_IDLE, TCTRL_ARGS,
    132   1.1      matt 		TCTRL_ACK, TCTRL_DATA } sc_state;
    133  1.28       uwe 	uint8_t		sc_cmdbuf[16];
    134  1.28       uwe 	uint8_t		sc_rspbuf[16];
    135  1.28       uwe 	uint8_t		sc_bitport;
    136  1.28       uwe 	uint8_t		sc_tft_on;
    137  1.28       uwe 	uint8_t		sc_op;
    138  1.28       uwe 	uint8_t		sc_cmdoff;
    139  1.28       uwe 	uint8_t		sc_cmdlen;
    140  1.28       uwe 	uint8_t		sc_rspoff;
    141  1.28       uwe 	uint8_t		sc_rsplen;
    142   1.4   garbled 	/* APM stuff */
    143   1.4   garbled #define APM_NEVENTS 16
    144   1.4   garbled 	struct	apm_event_info sc_event_list[APM_NEVENTS];
    145   1.4   garbled 	int	sc_event_count;
    146   1.4   garbled 	int	sc_event_ptr;
    147   1.4   garbled 	struct	selinfo sc_rsel;
    148  1.27  macallan 
    149   1.4   garbled 	/* ENVSYS stuff */
    150   1.4   garbled #define ENVSYS_NUMSENSORS 3
    151   1.4   garbled 	struct	evcnt sc_intrcnt;	/* interrupt counting */
    152  1.27  macallan 	struct	sysmon_envsys sc_sme;
    153  1.39   xtraeme 	envsys_data_t sc_sensor[ENVSYS_NUMSENSORS];
    154  1.28       uwe 
    155  1.31  macallan 	struct	sysmon_pswitch sc_sm_pbutton;	/* power button */
    156  1.31  macallan 	struct	sysmon_pswitch sc_sm_lid;	/* lid state */
    157  1.31  macallan 	struct	sysmon_pswitch sc_sm_ac;	/* AC adaptor presence */
    158  1.27  macallan 	int	sc_powerpressed;
    159  1.30  macallan 
    160  1.30  macallan 	/* hardware status stuff */
    161  1.30  macallan 	int sc_lid;	/* 1 - open, 0 - closed */
    162  1.30  macallan 	int sc_power_state;
    163  1.30  macallan 	int sc_spl;
    164  1.30  macallan 
    165  1.30  macallan 	/*
    166  1.30  macallan 	 * we call this when we detect connection or removal of an external
    167  1.30  macallan 	 * monitor. 0 for no monitor, !=0 for monitor present
    168  1.30  macallan 	 */
    169  1.30  macallan 	void (*sc_video_callback)(void *, int);
    170  1.30  macallan 	void *sc_video_callback_cookie;
    171  1.30  macallan 	int sc_extvga;
    172  1.30  macallan 
    173  1.30  macallan 	uint32_t sc_events;
    174  1.30  macallan 	struct proc *sc_thread;		/* event thread */
    175  1.30  macallan 
    176  1.30  macallan 	struct lock sc_requestlock;
    177   1.1      matt };
    178   1.1      matt 
    179   1.4   garbled #define TCTRL_STD_DEV		0
    180   1.4   garbled #define TCTRL_APMCTL_DEV	8
    181   1.1      matt 
    182  1.28       uwe static int tctrl_match(struct device *, struct cfdata *, void *);
    183  1.28       uwe static void tctrl_attach(struct device *, struct device *, void *);
    184  1.28       uwe static void tctrl_write(struct tctrl_softc *, bus_size_t, uint8_t);
    185  1.28       uwe static uint8_t tctrl_read(struct tctrl_softc *, bus_size_t);
    186  1.28       uwe static void tctrl_write_data(struct tctrl_softc *, uint8_t);
    187  1.28       uwe static uint8_t tctrl_read_data(struct tctrl_softc *);
    188  1.28       uwe static int tctrl_intr(void *);
    189  1.28       uwe static void tctrl_setup_bitport(void);
    190  1.28       uwe static void tctrl_setup_bitport_nop(void);
    191  1.28       uwe static void tctrl_read_ext_status(void);
    192  1.30  macallan static void tctrl_read_event_status(struct tctrl_softc *);
    193  1.28       uwe static int tctrl_apm_record_event(struct tctrl_softc *, u_int);
    194  1.28       uwe static void tctrl_init_lcd(void);
    195   1.1      matt 
    196  1.27  macallan static void tctrl_sensor_setup(struct tctrl_softc *);
    197  1.39   xtraeme static int tctrl_gtredata(struct sysmon_envsys *, envsys_data_t *);
    198  1.27  macallan 
    199  1.27  macallan static void tctrl_power_button_pressed(void *);
    200  1.31  macallan static void tctrl_lid_state(struct tctrl_softc *);
    201  1.31  macallan static void tctrl_ac_state(struct tctrl_softc *);
    202  1.31  macallan 
    203  1.27  macallan static int tctrl_powerfail(void *);
    204  1.27  macallan 
    205  1.30  macallan static void tctrl_create_event_thread(void *);
    206  1.30  macallan static void tctrl_event_thread(void *);
    207  1.30  macallan void tctrl_update_lcd(struct tctrl_softc *);
    208  1.30  macallan 
    209  1.30  macallan static void tctrl_lock(struct tctrl_softc *);
    210  1.30  macallan static void tctrl_unlock(struct tctrl_softc *);
    211  1.30  macallan 
    212  1.17   thorpej CFATTACH_DECL(tctrl, sizeof(struct tctrl_softc),
    213  1.18   thorpej     tctrl_match, tctrl_attach, NULL, NULL);
    214   1.1      matt 
    215  1.38  macallan static int tadpole_request(struct tctrl_req *, int, int);
    216  1.28       uwe 
    217   1.4   garbled /* XXX wtf is this? see i386/apm.c */
    218   1.4   garbled int tctrl_apm_evindex;
    219   1.1      matt 
    220   1.1      matt static int
    221  1.28       uwe tctrl_match(struct device *parent, struct cfdata *cf, void *aux)
    222   1.1      matt {
    223   1.1      matt 	union obio_attach_args *uoba = aux;
    224   1.1      matt 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    225   1.1      matt 
    226   1.1      matt 	if (uoba->uoba_isobio4 != 0) {
    227   1.1      matt 		return (0);
    228   1.1      matt 	}
    229   1.1      matt 
    230   1.1      matt 	/* Tadpole 3GX/3GS uses "uctrl" for the Tadpole Microcontroller
    231   1.1      matt 	 * (who's interface is off the TS102 PCMCIA controller but there
    232   1.1      matt 	 * exists a OpenProm for microcontroller interface).
    233   1.1      matt 	 */
    234   1.1      matt 	return strcmp("uctrl", sa->sa_name) == 0;
    235   1.1      matt }
    236   1.1      matt 
    237   1.1      matt static void
    238  1.28       uwe tctrl_attach(struct device *parent, struct device *self, void *aux)
    239   1.1      matt {
    240   1.1      matt 	struct tctrl_softc *sc = (void *)self;
    241   1.1      matt 	union obio_attach_args *uoba = aux;
    242   1.1      matt 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    243   1.2      matt 	unsigned int i, v;
    244   1.1      matt 
    245   1.1      matt 	/* We're living on a sbus slot that looks like an obio that
    246   1.1      matt 	 * looks like an sbus slot.
    247   1.1      matt 	 */
    248   1.1      matt 	sc->sc_memt = sa->sa_bustag;
    249  1.14        pk 	if (sbus_bus_map(sc->sc_memt,
    250  1.14        pk 			 sa->sa_slot,
    251  1.14        pk 			 sa->sa_offset - TS102_REG_UCTRL_INT,
    252  1.14        pk 			 sa->sa_size,
    253  1.14        pk 			 BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
    254   1.1      matt 		printf(": can't map registers\n");
    255   1.1      matt 		return;
    256   1.1      matt 	}
    257   1.1      matt 
    258   1.2      matt 	printf("\n");
    259   1.2      matt 
    260   1.1      matt 	sc->sc_tft_on = 1;
    261   1.2      matt 
    262   1.1      matt 	/* clear any pending data.
    263   1.1      matt 	 */
    264   1.1      matt 	for (i = 0; i < 10000; i++) {
    265   1.4   garbled 		if ((TS102_UCTRL_STS_RXNE_STA &
    266   1.4   garbled 		    tctrl_read(sc, TS102_REG_UCTRL_STS)) == 0) {
    267   1.1      matt 			break;
    268   1.1      matt 		}
    269   1.1      matt 		v = tctrl_read(sc, TS102_REG_UCTRL_DATA);
    270   1.2      matt 		tctrl_write(sc, TS102_REG_UCTRL_STS, TS102_UCTRL_STS_RXNE_STA);
    271   1.1      matt 	}
    272   1.1      matt 
    273   1.3        pk 	if (sa->sa_nintr != 0) {
    274  1.11        pk 		(void)bus_intr_establish(sc->sc_memt, sa->sa_pri, IPL_NONE,
    275  1.22        pk 					 tctrl_intr, sc);
    276  1.10       cgd 		evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    277  1.22        pk 				     sc->sc_dev.dv_xname, "intr");
    278   1.3        pk 	}
    279   1.2      matt 
    280  1.27  macallan 	/* See what the external status is */
    281  1.30  macallan 	sc->sc_ext_status = 0;
    282   1.4   garbled 	tctrl_read_ext_status();
    283   1.2      matt 	if (sc->sc_ext_status != 0) {
    284   1.2      matt 		const char *sep;
    285   1.1      matt 
    286   1.1      matt 		printf("%s: ", sc->sc_dev.dv_xname);
    287   1.2      matt 		v = sc->sc_ext_status;
    288   1.1      matt 		for (i = 0, sep = ""; v != 0; i++, v >>= 1) {
    289   1.1      matt 			if (v & 1) {
    290   1.1      matt 				printf("%s%s", sep, tctrl_ext_statuses[i]);
    291   1.1      matt 				sep = ", ";
    292   1.1      matt 			}
    293   1.1      matt 		}
    294   1.1      matt 		printf("\n");
    295   1.1      matt 	}
    296   1.1      matt 
    297  1.27  macallan 	/* Get a current of the control bitport */
    298   1.4   garbled 	tctrl_setup_bitport_nop();
    299   1.2      matt 	tctrl_write(sc, TS102_REG_UCTRL_INT,
    300   1.2      matt 		    TS102_UCTRL_INT_RXNE_REQ|TS102_UCTRL_INT_RXNE_MSK);
    301  1.30  macallan 	sc->sc_lid = (sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN) == 0;
    302  1.30  macallan 	sc->sc_power_state = PWR_RESUME;
    303  1.30  macallan 
    304  1.30  macallan 	sc->sc_extvga = (sc->sc_ext_status &
    305  1.30  macallan 	    TS102_EXT_STATUS_EXTERNAL_VGA_ATTACHED) != 0;
    306  1.30  macallan 	sc->sc_video_callback = NULL;
    307  1.30  macallan 
    308  1.30  macallan 
    309   1.4   garbled 	sc->sc_wantdata = 0;
    310   1.4   garbled 	sc->sc_event_count = 0;
    311  1.30  macallan 	sc->sc_ext_pending = 0;
    312  1.30  macallan 		sc->sc_ext_pending = 0;
    313   1.4   garbled 
    314  1.30  macallan 	lockinit(&sc->sc_requestlock, PUSER, "tctrl_req", 0, 0);
    315  1.27  macallan 	/* setup sensors and register the power button */
    316  1.27  macallan 	tctrl_sensor_setup(sc);
    317  1.31  macallan 	tctrl_lid_state(sc);
    318  1.31  macallan 	tctrl_ac_state(sc);
    319  1.28       uwe 
    320   1.6   garbled 	/* initialize the LCD */
    321   1.6   garbled 	tctrl_init_lcd();
    322   1.6   garbled 
    323   1.6   garbled 	/* initialize sc_lcdstate */
    324   1.6   garbled 	sc->sc_lcdstate = 0;
    325  1.30  macallan 	sc->sc_lcdwanted = 0;
    326  1.30  macallan 	tadpole_set_lcd(2, 0);
    327  1.30  macallan 
    328  1.30  macallan 	/* fire up the LCD event thread */
    329  1.30  macallan 	sc->sc_events = 0;
    330  1.30  macallan 	kthread_create(tctrl_create_event_thread, sc);
    331   1.1      matt }
    332   1.1      matt 
    333   1.1      matt static int
    334  1.28       uwe tctrl_intr(void *arg)
    335   1.1      matt {
    336   1.1      matt 	struct tctrl_softc *sc = arg;
    337   1.1      matt 	unsigned int v, d;
    338   1.1      matt 	int progress = 0;
    339   1.1      matt 
    340   1.1      matt     again:
    341   1.1      matt 	/* find out the cause(s) of the interrupt */
    342  1.12    toddpw 	v = tctrl_read(sc, TS102_REG_UCTRL_STS) & TS102_UCTRL_STS_MASK;
    343   1.1      matt 
    344   1.1      matt 	/* clear the cause(s) of the interrupt */
    345   1.1      matt 	tctrl_write(sc, TS102_REG_UCTRL_STS, v);
    346   1.1      matt 
    347   1.2      matt 	v &= ~(TS102_UCTRL_STS_RXO_STA|TS102_UCTRL_STS_TXE_STA);
    348   1.1      matt 	if (sc->sc_cmdoff >= sc->sc_cmdlen) {
    349   1.2      matt 		v &= ~TS102_UCTRL_STS_TXNF_STA;
    350  1.28       uwe 		if (tctrl_read(sc, TS102_REG_UCTRL_INT) &
    351  1.27  macallan 		    TS102_UCTRL_INT_TXNF_REQ) {
    352   1.4   garbled 			tctrl_write(sc, TS102_REG_UCTRL_INT, 0);
    353   1.4   garbled 			progress = 1;
    354   1.4   garbled 		}
    355   1.1      matt 	}
    356   1.4   garbled 	if ((v == 0) && ((sc->sc_flags & TCTRL_SEND_REQUEST) == 0 ||
    357   1.4   garbled 	    sc->sc_state != TCTRL_IDLE)) {
    358   1.4   garbled 		wakeup(sc);
    359   1.1      matt 		return progress;
    360   1.1      matt 	}
    361   1.1      matt 
    362   1.1      matt 	progress = 1;
    363   1.2      matt 	if (v & TS102_UCTRL_STS_RXNE_STA) {
    364   1.1      matt 		d = tctrl_read_data(sc);
    365   1.1      matt 		switch (sc->sc_state) {
    366   1.1      matt 		case TCTRL_IDLE:
    367   1.2      matt 			if (d == 0xfa) {
    368  1.30  macallan 				/*
    369  1.30  macallan 				 * external event,
    370  1.30  macallan 				 * set a flag and wakeup the event thread
    371  1.30  macallan 				 */
    372  1.30  macallan 				sc->sc_ext_pending = 1;
    373   1.2      matt 			} else {
    374   1.2      matt 				printf("%s: (op=0x%02x): unexpected data (0x%02x)\n",
    375   1.2      matt 					sc->sc_dev.dv_xname, sc->sc_op, d);
    376   1.1      matt 			}
    377   1.2      matt 			goto again;
    378   1.1      matt 		case TCTRL_ACK:
    379   1.1      matt 			if (d != 0xfe) {
    380   1.2      matt 				printf("%s: (op=0x%02x): unexpected ack value (0x%02x)\n",
    381   1.1      matt 					sc->sc_dev.dv_xname, sc->sc_op, d);
    382   1.1      matt 			}
    383   1.4   garbled #ifdef TCTRLDEBUG
    384   1.2      matt 			printf(" ack=0x%02x", d);
    385   1.2      matt #endif
    386   1.2      matt 			sc->sc_rsplen--;
    387   1.2      matt 			sc->sc_rspoff = 0;
    388   1.1      matt 			sc->sc_state = sc->sc_rsplen ? TCTRL_DATA : TCTRL_IDLE;
    389   1.4   garbled 			sc->sc_wantdata = sc->sc_rsplen ? 1 : 0;
    390   1.4   garbled #ifdef TCTRLDEBUG
    391   1.2      matt 			if (sc->sc_rsplen > 0) {
    392   1.2      matt 				printf(" [data(%u)]", sc->sc_rsplen);
    393   1.2      matt 			} else {
    394   1.2      matt 				printf(" [idle]\n");
    395   1.2      matt 			}
    396   1.2      matt #endif
    397   1.2      matt 			goto again;
    398   1.1      matt 		case TCTRL_DATA:
    399   1.1      matt 			sc->sc_rspbuf[sc->sc_rspoff++] = d;
    400   1.4   garbled #ifdef TCTRLDEBUG
    401   1.2      matt 			printf(" [%d]=0x%02x", sc->sc_rspoff-1, d);
    402   1.2      matt #endif
    403   1.1      matt 			if (sc->sc_rspoff == sc->sc_rsplen) {
    404   1.4   garbled #ifdef TCTRLDEBUG
    405   1.2      matt 				printf(" [idle]\n");
    406   1.2      matt #endif
    407   1.1      matt 				sc->sc_state = TCTRL_IDLE;
    408   1.4   garbled 				sc->sc_wantdata = 0;
    409   1.1      matt 			}
    410   1.2      matt 			goto again;
    411   1.1      matt 		default:
    412   1.1      matt 			printf("%s: (op=0x%02x): unexpected data (0x%02x) in state %d\n",
    413   1.1      matt 			       sc->sc_dev.dv_xname, sc->sc_op, d, sc->sc_state);
    414   1.2      matt 			goto again;
    415   1.1      matt 		}
    416   1.1      matt 	}
    417   1.4   garbled 	if ((sc->sc_state == TCTRL_IDLE && sc->sc_wantdata == 0) ||
    418   1.4   garbled 	    sc->sc_flags & TCTRL_SEND_REQUEST) {
    419   1.4   garbled 		if (sc->sc_flags & TCTRL_SEND_REQUEST) {
    420   1.4   garbled 			sc->sc_flags &= ~TCTRL_SEND_REQUEST;
    421   1.4   garbled 			sc->sc_wantdata = 1;
    422   1.4   garbled 		}
    423   1.1      matt 		if (sc->sc_cmdlen > 0) {
    424   1.1      matt 			tctrl_write(sc, TS102_REG_UCTRL_INT,
    425   1.1      matt 				tctrl_read(sc, TS102_REG_UCTRL_INT)
    426   1.1      matt 				|TS102_UCTRL_INT_TXNF_MSK
    427   1.1      matt 				|TS102_UCTRL_INT_TXNF_REQ);
    428   1.1      matt 			v = tctrl_read(sc, TS102_REG_UCTRL_STS);
    429   1.1      matt 		}
    430   1.1      matt 	}
    431   1.2      matt 	if ((sc->sc_cmdoff < sc->sc_cmdlen) && (v & TS102_UCTRL_STS_TXNF_STA)) {
    432   1.1      matt 		tctrl_write_data(sc, sc->sc_cmdbuf[sc->sc_cmdoff++]);
    433   1.4   garbled #ifdef TCTRLDEBUG
    434   1.2      matt 		if (sc->sc_cmdoff == 1) {
    435   1.2      matt 			printf("%s: op=0x%02x(l=%u)", sc->sc_dev.dv_xname,
    436   1.2      matt 				sc->sc_cmdbuf[0], sc->sc_rsplen);
    437   1.2      matt 		} else {
    438   1.2      matt 			printf(" [%d]=0x%02x", sc->sc_cmdoff-1,
    439   1.2      matt 				sc->sc_cmdbuf[sc->sc_cmdoff-1]);
    440   1.2      matt 		}
    441   1.2      matt #endif
    442   1.1      matt 		if (sc->sc_cmdoff == sc->sc_cmdlen) {
    443   1.1      matt 			sc->sc_state = sc->sc_rsplen ? TCTRL_ACK : TCTRL_IDLE;
    444   1.4   garbled #ifdef TCTRLDEBUG
    445   1.2      matt 			printf(" %s", sc->sc_rsplen ? "[ack]" : "[idle]\n");
    446   1.2      matt #endif
    447   1.2      matt 			if (sc->sc_cmdoff == 1) {
    448   1.2      matt 				sc->sc_op = sc->sc_cmdbuf[0];
    449   1.2      matt 			}
    450   1.1      matt 			tctrl_write(sc, TS102_REG_UCTRL_INT,
    451   1.1      matt 				tctrl_read(sc, TS102_REG_UCTRL_INT)
    452   1.1      matt 				& (~TS102_UCTRL_INT_TXNF_MSK
    453   1.1      matt 				   |TS102_UCTRL_INT_TXNF_REQ));
    454   1.1      matt 		} else if (sc->sc_state == TCTRL_IDLE) {
    455   1.1      matt 			sc->sc_op = sc->sc_cmdbuf[0];
    456   1.1      matt 			sc->sc_state = TCTRL_ARGS;
    457   1.4   garbled #ifdef TCTRLDEBUG
    458   1.2      matt 			printf(" [args]");
    459   1.2      matt #endif
    460   1.1      matt 		}
    461   1.1      matt 	}
    462   1.1      matt 	goto again;
    463   1.1      matt }
    464   1.1      matt 
    465   1.1      matt static void
    466   1.4   garbled tctrl_setup_bitport_nop(void)
    467   1.4   garbled {
    468   1.4   garbled 	struct tctrl_softc *sc;
    469   1.4   garbled 	struct tctrl_req req;
    470   1.4   garbled 	int s;
    471  1.28       uwe 
    472   1.4   garbled 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
    473   1.4   garbled 	req.cmdbuf[0] = TS102_OP_CTL_BITPORT;
    474   1.4   garbled 	req.cmdbuf[1] = 0xff;
    475  1.30  macallan 	req.cmdbuf[2] = 0x00;
    476   1.4   garbled 	req.cmdlen = 3;
    477   1.4   garbled 	req.rsplen = 2;
    478  1.38  macallan 	tadpole_request(&req, 1, 0);
    479   1.4   garbled 	s = splts102();
    480   1.4   garbled 	sc->sc_bitport = (req.rspbuf[0] & req.cmdbuf[1]) ^ req.cmdbuf[2];
    481   1.4   garbled 	splx(s);
    482   1.4   garbled }
    483   1.4   garbled 
    484   1.4   garbled static void
    485   1.4   garbled tctrl_setup_bitport(void)
    486   1.1      matt {
    487   1.4   garbled 	struct tctrl_softc *sc;
    488   1.4   garbled 	struct tctrl_req req;
    489   1.4   garbled 	int s;
    490  1.28       uwe 
    491   1.4   garbled 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
    492   1.4   garbled 	s = splts102();
    493  1.30  macallan 	req.cmdbuf[2] = 0;
    494   1.4   garbled 	if ((sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN)
    495   1.4   garbled 	    || (!sc->sc_tft_on)) {
    496   1.4   garbled 		req.cmdbuf[2] = TS102_BITPORT_TFTPWR;
    497   1.4   garbled 	}
    498   1.4   garbled 	req.cmdbuf[0] = TS102_OP_CTL_BITPORT;
    499   1.4   garbled 	req.cmdbuf[1] = ~TS102_BITPORT_TFTPWR;
    500   1.4   garbled 	req.cmdlen = 3;
    501   1.4   garbled 	req.rsplen = 2;
    502  1.38  macallan 	tadpole_request(&req, 1, 0);
    503   1.4   garbled 	s = splts102();
    504   1.4   garbled 	sc->sc_bitport = (req.rspbuf[0] & req.cmdbuf[1]) ^ req.cmdbuf[2];
    505   1.4   garbled 	splx(s);
    506   1.4   garbled }
    507   1.4   garbled 
    508   1.6   garbled /*
    509   1.6   garbled  * The tadpole microcontroller is not preprogrammed with icon
    510   1.6   garbled  * representations.  The machine boots with the DC-IN light as
    511   1.6   garbled  * a blank (all 0x00) and the other lights, as 4 rows of horizontal
    512   1.6   garbled  * bars.  The below code initializes the icons in the system to
    513   1.6   garbled  * sane values.  Some of these icons could be used for any purpose
    514   1.6   garbled  * desired, namely the pcmcia, LAN and WAN lights.  For the disk spinner,
    515   1.6   garbled  * only the backslash is unprogrammed.  (sigh)
    516   1.6   garbled  *
    517   1.6   garbled  * programming the icons is simple.  It is a 5x8 matrix, which each row a
    518   1.6   garbled  * bitfield in the order 0x10 0x08 0x04 0x02 0x01.
    519   1.6   garbled  */
    520   1.6   garbled 
    521   1.6   garbled static void
    522   1.6   garbled tctrl_init_lcd(void)
    523   1.6   garbled {
    524   1.6   garbled 	struct tctrl_req req;
    525   1.6   garbled 
    526   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    527   1.6   garbled 	req.cmdlen = 11;
    528   1.6   garbled 	req.rsplen = 1;
    529   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    530   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_DC_GOOD;
    531   1.6   garbled 	req.cmdbuf[3] =  0x00;	/* ..... */
    532   1.6   garbled 	req.cmdbuf[4] =  0x00;	/* ..... */
    533   1.6   garbled 	req.cmdbuf[5] =  0x1f;	/* XXXXX */
    534   1.6   garbled 	req.cmdbuf[6] =  0x00;	/* ..... */
    535   1.6   garbled 	req.cmdbuf[7] =  0x15;	/* X.X.X */
    536   1.6   garbled 	req.cmdbuf[8] =  0x00;	/* ..... */
    537   1.6   garbled 	req.cmdbuf[9] =  0x00;	/* ..... */
    538   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    539  1.38  macallan 	tadpole_request(&req, 1, 0);
    540   1.6   garbled 
    541   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    542   1.6   garbled 	req.cmdlen = 11;
    543   1.6   garbled 	req.rsplen = 1;
    544   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    545   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_BACKSLASH;
    546   1.6   garbled 	req.cmdbuf[3] =  0x00;	/* ..... */
    547   1.6   garbled 	req.cmdbuf[4] =  0x10;	/* X.... */
    548   1.6   garbled 	req.cmdbuf[5] =  0x08;	/* .X... */
    549   1.6   garbled 	req.cmdbuf[6] =  0x04;	/* ..X.. */
    550   1.6   garbled 	req.cmdbuf[7] =  0x02;	/* ...X. */
    551   1.6   garbled 	req.cmdbuf[8] =  0x01;	/* ....X */
    552   1.6   garbled 	req.cmdbuf[9] =  0x00;	/* ..... */
    553   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    554  1.38  macallan 	tadpole_request(&req, 1, 0);
    555   1.6   garbled 
    556   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    557   1.6   garbled 	req.cmdlen = 11;
    558   1.6   garbled 	req.rsplen = 1;
    559   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    560   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_WAN1;
    561   1.6   garbled 	req.cmdbuf[3] =  0x0c;	/* .XXX. */
    562   1.6   garbled 	req.cmdbuf[4] =  0x16;	/* X.XX. */
    563   1.6   garbled 	req.cmdbuf[5] =  0x10;	/* X.... */
    564   1.6   garbled 	req.cmdbuf[6] =  0x15;	/* X.X.X */
    565   1.6   garbled 	req.cmdbuf[7] =  0x10;	/* X.... */
    566   1.6   garbled 	req.cmdbuf[8] =  0x16;	/* X.XX. */
    567   1.6   garbled 	req.cmdbuf[9] =  0x0c;	/* .XXX. */
    568   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    569  1.38  macallan 	tadpole_request(&req, 1, 0);
    570   1.6   garbled 
    571   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    572   1.6   garbled 	req.cmdlen = 11;
    573   1.6   garbled 	req.rsplen = 1;
    574   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    575   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_WAN2;
    576   1.6   garbled 	req.cmdbuf[3] =  0x0c;	/* .XXX. */
    577   1.6   garbled 	req.cmdbuf[4] =  0x0d;	/* .XX.X */
    578   1.6   garbled 	req.cmdbuf[5] =  0x01;	/* ....X */
    579   1.6   garbled 	req.cmdbuf[6] =  0x15;	/* X.X.X */
    580   1.6   garbled 	req.cmdbuf[7] =  0x01;	/* ....X */
    581   1.6   garbled 	req.cmdbuf[8] =  0x0d;	/* .XX.X */
    582   1.6   garbled 	req.cmdbuf[9] =  0x0c;	/* .XXX. */
    583   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    584  1.38  macallan 	tadpole_request(&req, 1, 0);
    585   1.6   garbled 
    586   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    587   1.6   garbled 	req.cmdlen = 11;
    588   1.6   garbled 	req.rsplen = 1;
    589   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    590   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_LAN1;
    591   1.6   garbled 	req.cmdbuf[3] =  0x00;	/* ..... */
    592   1.6   garbled 	req.cmdbuf[4] =  0x04;	/* ..X.. */
    593   1.6   garbled 	req.cmdbuf[5] =  0x08;	/* .X... */
    594   1.6   garbled 	req.cmdbuf[6] =  0x13;	/* X..XX */
    595   1.6   garbled 	req.cmdbuf[7] =  0x08;	/* .X... */
    596   1.6   garbled 	req.cmdbuf[8] =  0x04;	/* ..X.. */
    597   1.6   garbled 	req.cmdbuf[9] =  0x00;	/* ..... */
    598   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    599  1.38  macallan 	tadpole_request(&req, 1, 0);
    600   1.6   garbled 
    601   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    602   1.6   garbled 	req.cmdlen = 11;
    603   1.6   garbled 	req.rsplen = 1;
    604   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    605   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_LAN2;
    606   1.6   garbled 	req.cmdbuf[3] =  0x00;	/* ..... */
    607   1.6   garbled 	req.cmdbuf[4] =  0x04;	/* ..X.. */
    608   1.6   garbled 	req.cmdbuf[5] =  0x02;	/* ...X. */
    609   1.6   garbled 	req.cmdbuf[6] =  0x19;	/* XX..X */
    610   1.6   garbled 	req.cmdbuf[7] =  0x02;	/* ...X. */
    611   1.6   garbled 	req.cmdbuf[8] =  0x04;	/* ..X.. */
    612   1.6   garbled 	req.cmdbuf[9] =  0x00;	/* ..... */
    613   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    614  1.38  macallan 	tadpole_request(&req, 1, 0);
    615   1.6   garbled 
    616   1.6   garbled 	req.cmdbuf[0] = TS102_OP_BLK_DEF_SPCL_CHAR;
    617   1.6   garbled 	req.cmdlen = 11;
    618   1.6   garbled 	req.rsplen = 1;
    619   1.6   garbled 	req.cmdbuf[1] = 0x08;	/*len*/
    620   1.6   garbled 	req.cmdbuf[2] = TS102_BLK_OFF_DEF_PCMCIA;
    621   1.6   garbled 	req.cmdbuf[3] =  0x00;	/* ..... */
    622   1.6   garbled 	req.cmdbuf[4] =  0x0c;	/* .XXX. */
    623   1.6   garbled 	req.cmdbuf[5] =  0x1f;	/* XXXXX */
    624   1.6   garbled 	req.cmdbuf[6] =  0x1f;	/* XXXXX */
    625   1.6   garbled 	req.cmdbuf[7] =  0x1f;	/* XXXXX */
    626   1.6   garbled 	req.cmdbuf[8] =  0x1f;	/* XXXXX */
    627   1.6   garbled 	req.cmdbuf[9] =  0x00;	/* ..... */
    628   1.6   garbled 	req.cmdbuf[10] = 0x00;	/* ..... */
    629  1.38  macallan 	tadpole_request(&req, 1, 0);
    630   1.6   garbled }
    631   1.6   garbled 
    632  1.30  macallan /* sc_lcdwanted -> lcd_state */
    633   1.6   garbled void
    634  1.30  macallan tctrl_update_lcd(struct tctrl_softc *sc)
    635   1.6   garbled {
    636   1.6   garbled 	struct tctrl_req req;
    637   1.6   garbled 	int s;
    638  1.27  macallan 
    639  1.30  macallan 	s = splhigh();
    640  1.30  macallan 	if (sc->sc_lcdwanted == sc->sc_lcdstate) {
    641   1.6   garbled 		splx(s);
    642   1.6   garbled 		return;
    643   1.6   garbled 	}
    644  1.30  macallan 	sc->sc_lcdstate = sc->sc_lcdwanted;
    645  1.30  macallan 	splx(s);
    646  1.30  macallan 
    647   1.6   garbled 	/*
    648   1.6   garbled 	 * the mask setup on this particular command is *very* bizzare
    649   1.6   garbled 	 * and totally undocumented.
    650   1.6   garbled 	 */
    651   1.6   garbled 	req.cmdbuf[0] = TS102_OP_CTL_LCD;
    652  1.28       uwe 
    653  1.30  macallan 	/* leave caps-lock alone */
    654  1.30  macallan 	req.cmdbuf[2] = (u_int8_t)(sc->sc_lcdstate & 0xfe);
    655  1.30  macallan 	req.cmdbuf[3] = (u_int8_t)((sc->sc_lcdstate & 0x100)>>8);
    656  1.30  macallan 
    657  1.30  macallan 	req.cmdbuf[1] = 1;
    658  1.30  macallan 	req.cmdbuf[4] = 0;
    659  1.30  macallan 
    660   1.6   garbled 
    661  1.13       wiz 	/* XXX this thing is weird.... */
    662   1.6   garbled 	req.cmdlen = 3;
    663   1.6   garbled 	req.rsplen = 2;
    664  1.30  macallan 
    665  1.27  macallan 	/* below are the values one would expect but which won't work */
    666   1.6   garbled #if 0
    667   1.6   garbled 	req.cmdlen = 5;
    668   1.6   garbled 	req.rsplen = 4;
    669   1.6   garbled #endif
    670  1.38  macallan 	tadpole_request(&req, 1, 0);
    671  1.30  macallan }
    672  1.27  macallan 
    673  1.30  macallan 
    674  1.30  macallan /*
    675  1.30  macallan  * set the blinken-lights on the lcd.  what:
    676  1.30  macallan  * what = 0 off,  what = 1 on,  what = 2 toggle
    677  1.30  macallan  */
    678  1.30  macallan 
    679  1.30  macallan void
    680  1.30  macallan tadpole_set_lcd(int what, unsigned short which)
    681  1.30  macallan {
    682  1.30  macallan 	struct tctrl_softc *sc;
    683  1.30  macallan 	int s;
    684  1.30  macallan 
    685  1.30  macallan 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
    686  1.30  macallan 
    687  1.30  macallan 	s = splhigh();
    688  1.30  macallan 	switch (what) {
    689  1.30  macallan 		case 0:
    690  1.30  macallan 			sc->sc_lcdwanted &= ~which;
    691  1.30  macallan 			break;
    692  1.30  macallan 		case 1:
    693  1.30  macallan 			sc->sc_lcdwanted |= which;
    694  1.30  macallan 			break;
    695  1.30  macallan 		case 2:
    696  1.30  macallan 			sc->sc_lcdwanted ^= which;
    697  1.30  macallan 			break;
    698  1.30  macallan 	}
    699   1.6   garbled 	splx(s);
    700   1.6   garbled }
    701   1.6   garbled 
    702   1.4   garbled static void
    703   1.4   garbled tctrl_read_ext_status(void)
    704   1.4   garbled {
    705   1.4   garbled 	struct tctrl_softc *sc;
    706   1.4   garbled 	struct tctrl_req req;
    707   1.4   garbled 	int s;
    708  1.28       uwe 
    709   1.4   garbled 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
    710   1.4   garbled 	req.cmdbuf[0] = TS102_OP_RD_EXT_STATUS;
    711   1.4   garbled 	req.cmdlen = 1;
    712   1.4   garbled 	req.rsplen = 3;
    713   1.4   garbled #ifdef TCTRLDEBUG
    714   1.4   garbled 	printf("pre read: sc->sc_ext_status = 0x%x\n", sc->sc_ext_status);
    715   1.4   garbled #endif
    716  1.38  macallan 	tadpole_request(&req, 1, 0);
    717   1.4   garbled 	s = splts102();
    718  1.30  macallan 	sc->sc_ext_status = (req.rspbuf[0] << 8) + req.rspbuf[1];
    719   1.4   garbled 	splx(s);
    720   1.4   garbled #ifdef TCTRLDEBUG
    721   1.4   garbled 	printf("post read: sc->sc_ext_status = 0x%x\n", sc->sc_ext_status);
    722   1.4   garbled #endif
    723   1.4   garbled }
    724   1.4   garbled 
    725   1.4   garbled /*
    726   1.4   garbled  * return 0 if the user will notice and handle the event,
    727   1.4   garbled  * return 1 if the kernel driver should do so.
    728   1.4   garbled  */
    729   1.4   garbled static int
    730  1.28       uwe tctrl_apm_record_event(struct tctrl_softc *sc, u_int event_type)
    731   1.4   garbled {
    732   1.4   garbled 	struct apm_event_info *evp;
    733   1.4   garbled 
    734   1.4   garbled 	if ((sc->sc_flags & TCTRL_APM_CTLOPEN) &&
    735   1.4   garbled 	    (sc->sc_event_count < APM_NEVENTS)) {
    736   1.4   garbled 		evp = &sc->sc_event_list[sc->sc_event_ptr];
    737   1.4   garbled 		sc->sc_event_count++;
    738   1.4   garbled 		sc->sc_event_ptr++;
    739   1.4   garbled 		sc->sc_event_ptr %= APM_NEVENTS;
    740   1.4   garbled 		evp->type = event_type;
    741   1.4   garbled 		evp->index = ++tctrl_apm_evindex;
    742  1.20  jdolecek 		selnotify(&sc->sc_rsel, 0);
    743   1.4   garbled 		return(sc->sc_flags & TCTRL_APM_CTLOPEN) ? 0 : 1;
    744   1.1      matt 	}
    745   1.4   garbled 	return(1);
    746   1.1      matt }
    747   1.1      matt 
    748   1.1      matt static void
    749  1.30  macallan tctrl_read_event_status(struct tctrl_softc *sc)
    750   1.1      matt {
    751   1.4   garbled 	struct tctrl_req req;
    752  1.30  macallan 	int s, lid;
    753  1.30  macallan 	uint32_t v;
    754  1.28       uwe 
    755   1.4   garbled 	req.cmdbuf[0] = TS102_OP_RD_EVENT_STATUS;
    756   1.4   garbled 	req.cmdlen = 1;
    757   1.4   garbled 	req.rsplen = 3;
    758  1.38  macallan 	tadpole_request(&req, 1, 0);
    759   1.4   garbled 	s = splts102();
    760   1.4   garbled 	v = req.rspbuf[0] * 256 + req.rspbuf[1];
    761  1.27  macallan #ifdef TCTRLDEBUG
    762  1.27  macallan 	printf("event: %x\n",v);
    763  1.27  macallan #endif
    764  1.28       uwe 	if (v & TS102_EVENT_STATUS_POWERON_BTN_PRESSED) {
    765  1.27  macallan 		printf("%s: Power button pressed\n",sc->sc_dev.dv_xname);
    766  1.27  macallan 		tctrl_powerfail(sc);
    767  1.27  macallan 	}
    768   1.4   garbled 	if (v & TS102_EVENT_STATUS_SHUTDOWN_REQUEST) {
    769   1.4   garbled 		printf("%s: SHUTDOWN REQUEST!\n", sc->sc_dev.dv_xname);
    770  1.30  macallan 		tctrl_powerfail(sc);
    771   1.4   garbled 	}
    772   1.4   garbled 	if (v & TS102_EVENT_STATUS_VERY_LOW_POWER_WARNING) {
    773   1.4   garbled /*printf("%s: VERY LOW POWER WARNING!\n", sc->sc_dev.dv_xname);*/
    774   1.4   garbled /* according to a tadpole header, and observation */
    775   1.4   garbled #ifdef TCTRLDEBUG
    776  1.28       uwe 		printf("%s: Battery charge level change\n",
    777  1.27  macallan 		    sc->sc_dev.dv_xname);
    778   1.4   garbled #endif
    779   1.1      matt 	}
    780   1.4   garbled 	if (v & TS102_EVENT_STATUS_LOW_POWER_WARNING) {
    781   1.4   garbled 		if (tctrl_apm_record_event(sc, APM_BATTERY_LOW))
    782   1.1      matt 			printf("%s: LOW POWER WARNING!\n", sc->sc_dev.dv_xname);
    783   1.4   garbled 	}
    784   1.4   garbled 	if (v & TS102_EVENT_STATUS_DC_STATUS_CHANGE) {
    785   1.4   garbled 		splx(s);
    786   1.4   garbled 		tctrl_read_ext_status();
    787  1.31  macallan 		tctrl_ac_state(sc);
    788   1.4   garbled 		s = splts102();
    789   1.4   garbled 		if (tctrl_apm_record_event(sc, APM_POWER_CHANGE))
    790   1.1      matt 			printf("%s: main power %s\n", sc->sc_dev.dv_xname,
    791   1.4   garbled 			    (sc->sc_ext_status &
    792   1.4   garbled 			    TS102_EXT_STATUS_MAIN_POWER_AVAILABLE) ?
    793   1.4   garbled 			    "restored" : "removed");
    794   1.4   garbled 	}
    795   1.4   garbled 	if (v & TS102_EVENT_STATUS_LID_STATUS_CHANGE) {
    796   1.4   garbled 		splx(s);
    797   1.4   garbled 		tctrl_read_ext_status();
    798  1.31  macallan 		tctrl_lid_state(sc);
    799   1.4   garbled 		tctrl_setup_bitport();
    800   1.4   garbled #ifdef TCTRLDEBUG
    801   1.4   garbled 		printf("%s: lid %s\n", sc->sc_dev.dv_xname,
    802   1.4   garbled 		    (sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN)
    803   1.4   garbled 		    ? "closed" : "opened");
    804   1.2      matt #endif
    805  1.30  macallan 		lid = (sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN) == 0;
    806  1.30  macallan 	}
    807  1.30  macallan 	if (v & TS102_EVENT_STATUS_EXTERNAL_VGA_STATUS_CHANGE) {
    808  1.30  macallan 		int vga;
    809  1.30  macallan 		splx(s);
    810  1.30  macallan 		tctrl_read_ext_status();
    811  1.30  macallan 		vga = (sc->sc_ext_status &
    812  1.30  macallan 		    TS102_EXT_STATUS_EXTERNAL_VGA_ATTACHED) != 0;
    813  1.30  macallan 		if (vga != sc->sc_extvga) {
    814  1.30  macallan 			sc->sc_extvga = vga;
    815  1.30  macallan 			if (sc->sc_video_callback != NULL) {
    816  1.30  macallan 				sc->sc_video_callback(
    817  1.30  macallan 				    sc->sc_video_callback_cookie,
    818  1.30  macallan 				    sc->sc_extvga);
    819  1.30  macallan 			}
    820  1.30  macallan 		}
    821  1.30  macallan 	}
    822  1.30  macallan #ifdef DIAGNOSTIC
    823  1.30  macallan 	if (v & TS102_EVENT_STATUS_EXT_MOUSE_STATUS_CHANGE) {
    824  1.30  macallan 		splx(s);
    825  1.30  macallan 		tctrl_read_ext_status();
    826  1.30  macallan 		if (sc->sc_ext_status &
    827  1.30  macallan 		    TS102_EXT_STATUS_EXTERNAL_MOUSE_ATTACHED) {
    828  1.30  macallan 			printf("tctrl: external mouse detected\n");
    829  1.30  macallan 		}
    830   1.1      matt 	}
    831  1.30  macallan #endif
    832  1.30  macallan 	sc->sc_ext_pending = 0;
    833   1.4   garbled 	splx(s);
    834   1.1      matt }
    835   1.1      matt 
    836  1.30  macallan static void
    837  1.30  macallan tctrl_lock(struct tctrl_softc *sc)
    838  1.30  macallan {
    839  1.30  macallan 
    840  1.30  macallan 	lockmgr(&sc->sc_requestlock, LK_EXCLUSIVE, NULL);
    841  1.30  macallan }
    842  1.30  macallan 
    843  1.30  macallan static void
    844  1.30  macallan tctrl_unlock(struct tctrl_softc *sc)
    845  1.30  macallan {
    846  1.30  macallan 
    847  1.30  macallan 	lockmgr(&sc->sc_requestlock, LK_RELEASE, NULL);
    848  1.30  macallan }
    849  1.30  macallan 
    850  1.30  macallan int
    851  1.38  macallan tadpole_request(struct tctrl_req *req, int spin, int sleep)
    852   1.1      matt {
    853   1.1      matt 	struct tctrl_softc *sc;
    854   1.1      matt 	int i, s;
    855   1.1      matt 
    856   1.1      matt 	if (tctrl_cd.cd_devs == NULL
    857   1.1      matt 	    || tctrl_cd.cd_ndevs == 0
    858   1.4   garbled 	    || tctrl_cd.cd_devs[TCTRL_STD_DEV] == NULL) {
    859  1.30  macallan 		return ENODEV;
    860   1.1      matt 	}
    861   1.1      matt 
    862   1.4   garbled 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
    863  1.30  macallan 	tctrl_lock(sc);
    864  1.30  macallan 
    865   1.4   garbled 	if (spin)
    866   1.4   garbled 		s = splhigh();
    867   1.4   garbled 	else
    868   1.4   garbled 		s = splts102();
    869   1.4   garbled 	sc->sc_flags |= TCTRL_SEND_REQUEST;
    870   1.4   garbled 	memcpy(sc->sc_cmdbuf, req->cmdbuf, req->cmdlen);
    871  1.30  macallan #ifdef DIAGNOSTIC
    872  1.30  macallan 	if (sc->sc_wantdata != 0) {
    873  1.30  macallan 		splx(s);
    874  1.30  macallan 		printf("tctrl: we lost the race\n");
    875  1.30  macallan 		tctrl_unlock(sc);
    876  1.30  macallan 		return EAGAIN;
    877  1.30  macallan 	}
    878  1.30  macallan #endif
    879   1.4   garbled 	sc->sc_wantdata = 1;
    880   1.4   garbled 	sc->sc_rsplen = req->rsplen;
    881   1.4   garbled 	sc->sc_cmdlen = req->cmdlen;
    882   1.4   garbled 	sc->sc_cmdoff = sc->sc_rspoff = 0;
    883   1.4   garbled 
    884   1.4   garbled 	/* we spin for certain commands, like poweroffs */
    885   1.4   garbled 	if (spin) {
    886   1.6   garbled /*		for (i = 0; i < 30000; i++) {*/
    887  1.30  macallan 		i = 0;
    888  1.30  macallan 		while ((sc->sc_wantdata == 1) && (i < 30000)) {
    889   1.4   garbled 			tctrl_intr(sc);
    890   1.4   garbled 			DELAY(1);
    891  1.30  macallan 			i++;
    892   1.4   garbled 		}
    893  1.30  macallan #ifdef DIAGNOSTIC
    894  1.30  macallan 		if (i >= 30000) {
    895  1.30  macallan 			printf("tctrl: timeout busy waiting for micro controller request!\n");
    896  1.30  macallan 			sc->sc_wantdata = 0;
    897  1.30  macallan 			splx(s);
    898  1.30  macallan 			tctrl_unlock(sc);
    899  1.30  macallan 			return EAGAIN;
    900  1.30  macallan 		}
    901  1.30  macallan #endif
    902   1.4   garbled 	} else {
    903  1.30  macallan 		int timeout = 5 * (sc->sc_rsplen + sc->sc_cmdlen);
    904   1.1      matt 		tctrl_intr(sc);
    905   1.5   garbled 		i = 0;
    906   1.5   garbled 		while (((sc->sc_rspoff != sc->sc_rsplen) ||
    907   1.5   garbled 		    (sc->sc_cmdoff != sc->sc_cmdlen)) &&
    908  1.30  macallan 		    (i < timeout))
    909  1.38  macallan 			if (sleep) {
    910   1.5   garbled 				tsleep(sc, PWAIT, "tctrl_data", 15);
    911   1.5   garbled 				i++;
    912  1.30  macallan 			} else
    913   1.4   garbled 				DELAY(1);
    914  1.30  macallan #ifdef DIAGNOSTIC
    915  1.30  macallan 		if (i >= timeout) {
    916  1.30  macallan 			printf("tctrl: timeout waiting for microcontroller request\n");
    917  1.30  macallan 			sc->sc_wantdata = 0;
    918  1.30  macallan 			splx(s);
    919  1.30  macallan 			tctrl_unlock(sc);
    920  1.30  macallan 			return EAGAIN;
    921  1.30  macallan 		}
    922  1.30  macallan #endif
    923   1.1      matt 	}
    924   1.5   garbled 	/*
    925   1.5   garbled 	 * we give the user a reasonable amount of time for a command
    926   1.5   garbled 	 * to complete.  If it doesn't complete in time, we hand them
    927   1.5   garbled 	 * garbage.  This is here to stop things like setting the
    928   1.5   garbled 	 * rsplen too long, and sleeping forever in a CMD_REQ ioctl.
    929   1.5   garbled 	 */
    930   1.5   garbled 	sc->sc_wantdata = 0;
    931   1.4   garbled 	memcpy(req->rspbuf, sc->sc_rspbuf, req->rsplen);
    932   1.1      matt 	splx(s);
    933  1.30  macallan 
    934  1.30  macallan 	tctrl_unlock(sc);
    935  1.30  macallan 	return 0;
    936   1.1      matt }
    937   1.1      matt 
    938   1.1      matt void
    939   1.4   garbled tadpole_powerdown(void)
    940   1.4   garbled {
    941   1.4   garbled 	struct tctrl_req req;
    942  1.28       uwe 
    943   1.4   garbled 	req.cmdbuf[0] = TS102_OP_ADMIN_POWER_OFF;
    944   1.4   garbled 	req.cmdlen = 1;
    945   1.4   garbled 	req.rsplen = 1;
    946  1.38  macallan 	tadpole_request(&req, 1, 0);
    947   1.4   garbled }
    948   1.4   garbled 
    949   1.4   garbled void
    950  1.28       uwe tadpole_set_video(int enabled)
    951   1.1      matt {
    952   1.1      matt 	struct tctrl_softc *sc;
    953   1.4   garbled 	struct tctrl_req req;
    954   1.1      matt 	int s;
    955   1.1      matt 
    956   1.4   garbled 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
    957   1.4   garbled 	while (sc->sc_wantdata != 0)
    958   1.4   garbled 		DELAY(1);
    959   1.4   garbled 	s = splts102();
    960   1.4   garbled 	if ((sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN && !enabled)
    961   1.4   garbled 	    || (sc->sc_tft_on)) {
    962   1.4   garbled 		req.cmdbuf[2] = TS102_BITPORT_TFTPWR;
    963   1.4   garbled 	} else {
    964   1.4   garbled 		req.cmdbuf[2] = 0;
    965   1.1      matt 	}
    966   1.4   garbled 	req.cmdbuf[0] = TS102_OP_CTL_BITPORT;
    967   1.4   garbled 	req.cmdbuf[1] = ~TS102_BITPORT_TFTPWR;
    968   1.4   garbled 	req.cmdlen = 3;
    969   1.4   garbled 	req.rsplen = 2;
    970   1.1      matt 
    971   1.1      matt 	if ((sc->sc_tft_on && !enabled) || (!sc->sc_tft_on && enabled)) {
    972   1.1      matt 		sc->sc_tft_on = enabled;
    973   1.1      matt 		if (sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN) {
    974   1.1      matt 			splx(s);
    975   1.1      matt 			return;
    976   1.1      matt 		}
    977  1.38  macallan 		tadpole_request(&req, 1, 0);
    978   1.4   garbled 		sc->sc_bitport =
    979   1.4   garbled 		    (req.rspbuf[0] & req.cmdbuf[1]) ^ req.cmdbuf[2];
    980   1.1      matt 	}
    981   1.1      matt 	splx(s);
    982   1.1      matt }
    983   1.1      matt 
    984   1.1      matt static void
    985  1.28       uwe tctrl_write_data(struct tctrl_softc *sc, uint8_t v)
    986   1.1      matt {
    987   1.1      matt 	unsigned int i;
    988   1.4   garbled 
    989   1.1      matt 	for (i = 0; i < 100; i++)  {
    990  1.28       uwe 		if (TS102_UCTRL_STS_TXNF_STA &
    991  1.27  macallan 		    tctrl_read(sc, TS102_REG_UCTRL_STS))
    992   1.1      matt 			break;
    993   1.1      matt 	}
    994   1.1      matt 	tctrl_write(sc, TS102_REG_UCTRL_DATA, v);
    995   1.1      matt }
    996   1.1      matt 
    997  1.28       uwe static uint8_t
    998  1.28       uwe tctrl_read_data(struct tctrl_softc *sc)
    999   1.4   garbled {
   1000   1.1      matt 	unsigned int i, v;
   1001   1.1      matt 
   1002   1.1      matt 	for (i = 0; i < 100000; i++) {
   1003  1.28       uwe 		if (TS102_UCTRL_STS_RXNE_STA &
   1004  1.27  macallan 		    tctrl_read(sc, TS102_REG_UCTRL_STS))
   1005   1.1      matt 			break;
   1006   1.1      matt 		DELAY(1);
   1007   1.1      matt 	}
   1008   1.1      matt 
   1009   1.1      matt 	v = tctrl_read(sc, TS102_REG_UCTRL_DATA);
   1010   1.2      matt 	tctrl_write(sc, TS102_REG_UCTRL_STS, TS102_UCTRL_STS_RXNE_STA);
   1011   1.1      matt 	return v;
   1012   1.1      matt }
   1013   1.1      matt 
   1014  1.28       uwe static uint8_t
   1015  1.28       uwe tctrl_read(struct tctrl_softc *sc, bus_size_t off)
   1016   1.1      matt {
   1017   1.4   garbled 
   1018   1.2      matt 	sc->sc_junk = bus_space_read_1(sc->sc_memt, sc->sc_memh, off);
   1019   1.1      matt 	return sc->sc_junk;
   1020   1.1      matt }
   1021   1.1      matt 
   1022   1.1      matt static void
   1023  1.28       uwe tctrl_write(struct tctrl_softc *sc, bus_size_t off, uint8_t v)
   1024   1.1      matt {
   1025   1.4   garbled 
   1026   1.1      matt 	sc->sc_junk = v;
   1027   1.2      matt 	bus_space_write_1(sc->sc_memt, sc->sc_memh, off, v);
   1028   1.4   garbled }
   1029   1.4   garbled 
   1030   1.4   garbled int
   1031  1.29  christos tctrlopen(dev_t dev, int flags, int mode, struct lwp *l)
   1032   1.4   garbled {
   1033   1.4   garbled 	int unit = (minor(dev)&0xf0);
   1034   1.4   garbled 	int ctl = (minor(dev)&0x0f);
   1035   1.4   garbled 	struct tctrl_softc *sc;
   1036   1.4   garbled 
   1037   1.4   garbled 	if (unit >= tctrl_cd.cd_ndevs)
   1038   1.4   garbled 		return(ENXIO);
   1039   1.4   garbled 	sc = tctrl_cd.cd_devs[TCTRL_STD_DEV];
   1040   1.4   garbled 	if (!sc)
   1041   1.4   garbled 		return(ENXIO);
   1042  1.28       uwe 
   1043   1.4   garbled 	switch (ctl) {
   1044   1.4   garbled 	case TCTRL_STD_DEV:
   1045   1.4   garbled 		break;
   1046   1.4   garbled 	case TCTRL_APMCTL_DEV:
   1047   1.4   garbled 		if (!(flags & FWRITE))
   1048   1.4   garbled 			return(EINVAL);
   1049   1.4   garbled 		if (sc->sc_flags & TCTRL_APM_CTLOPEN)
   1050   1.4   garbled 			return(EBUSY);
   1051   1.4   garbled 		sc->sc_flags |= TCTRL_APM_CTLOPEN;
   1052   1.4   garbled 		break;
   1053   1.4   garbled 	default:
   1054   1.4   garbled 		return(ENXIO);
   1055   1.4   garbled 		break;
   1056   1.4   garbled 	}
   1057   1.4   garbled 
   1058   1.4   garbled 	return(0);
   1059   1.4   garbled }
   1060   1.4   garbled 
   1061   1.4   garbled int
   1062  1.29  christos tctrlclose(dev_t dev, int flags, int mode, struct lwp *l)
   1063   1.4   garbled {
   1064   1.4   garbled 	int ctl = (minor(dev)&0x0f);
   1065   1.4   garbled 	struct tctrl_softc *sc;
   1066   1.4   garbled 
   1067   1.4   garbled 	sc = tctrl_cd.cd_devs[TCTRL_STD_DEV];
   1068   1.4   garbled 	if (!sc)
   1069   1.4   garbled 		return(ENXIO);
   1070   1.4   garbled 
   1071   1.4   garbled 	switch (ctl) {
   1072   1.4   garbled 	case TCTRL_STD_DEV:
   1073   1.4   garbled 		break;
   1074   1.4   garbled 	case TCTRL_APMCTL_DEV:
   1075   1.4   garbled 		sc->sc_flags &= ~TCTRL_APM_CTLOPEN;
   1076   1.4   garbled 		break;
   1077   1.4   garbled 	}
   1078   1.4   garbled 	return(0);
   1079   1.4   garbled }
   1080   1.4   garbled 
   1081   1.4   garbled int
   1082  1.37  christos tctrlioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
   1083   1.4   garbled {
   1084   1.4   garbled 	struct tctrl_req req, *reqn;
   1085  1.28       uwe 	struct tctrl_pwr *pwrreq;
   1086   1.4   garbled 	struct apm_power_info *powerp;
   1087   1.4   garbled 	struct apm_event_info *evp;
   1088   1.4   garbled 	struct tctrl_softc *sc;
   1089   1.4   garbled 	int i;
   1090  1.28       uwe 	uint8_t c;
   1091   1.4   garbled 
   1092   1.4   garbled 	if (tctrl_cd.cd_devs == NULL
   1093   1.4   garbled 	    || tctrl_cd.cd_ndevs == 0
   1094   1.4   garbled 	    || tctrl_cd.cd_devs[TCTRL_STD_DEV] == NULL) {
   1095   1.4   garbled 		return ENXIO;
   1096   1.4   garbled 	}
   1097   1.4   garbled 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
   1098   1.4   garbled         switch (cmd) {
   1099   1.4   garbled 
   1100   1.4   garbled 	case APM_IOC_STANDBY:
   1101  1.30  macallan 		/* turn off backlight and so on ? */
   1102  1.30  macallan 
   1103  1.30  macallan 		return 0; /* for now */
   1104   1.4   garbled 
   1105   1.4   garbled 	case APM_IOC_SUSPEND:
   1106  1.30  macallan 		/* not sure what to do here - we can't really suspend */
   1107  1.30  macallan 
   1108  1.30  macallan 		return 0; /* for now */
   1109   1.4   garbled 
   1110  1.19  takemura 	case OAPM_IOC_GETPOWER:
   1111   1.4   garbled 	case APM_IOC_GETPOWER:
   1112   1.4   garbled 		powerp = (struct apm_power_info *)data;
   1113   1.4   garbled 		req.cmdbuf[0] = TS102_OP_RD_INT_CHARGE_RATE;
   1114   1.4   garbled 		req.cmdlen = 1;
   1115   1.4   garbled 		req.rsplen = 2;
   1116  1.38  macallan 		tadpole_request(&req, 0, l->l_proc ? 1 : 0);
   1117   1.4   garbled 		if (req.rspbuf[0] > 0x00)
   1118   1.4   garbled 			powerp->battery_state = APM_BATT_CHARGING;
   1119   1.4   garbled 		req.cmdbuf[0] = TS102_OP_RD_INT_CHARGE_LEVEL;
   1120   1.4   garbled 		req.cmdlen = 1;
   1121   1.4   garbled 		req.rsplen = 3;
   1122  1.38  macallan 		tadpole_request(&req, 0, l->l_proc ? 1 : 0);
   1123   1.4   garbled 		c = req.rspbuf[0];
   1124   1.4   garbled 		powerp->battery_life = c;
   1125   1.6   garbled 		if (c > 0x70)	/* the tadpole sometimes dips below zero, and */
   1126   1.6   garbled 			c = 0;	/* into the 255 range. */
   1127   1.4   garbled 		powerp->minutes_left = (45 * c) / 100; /* XXX based on 45 min */
   1128   1.4   garbled 		if (powerp->battery_state != APM_BATT_CHARGING) {
   1129   1.4   garbled 			if (c < 0x20)
   1130   1.4   garbled 				powerp->battery_state = APM_BATT_CRITICAL;
   1131   1.4   garbled 			else if (c < 0x40)
   1132   1.4   garbled 				powerp->battery_state = APM_BATT_LOW;
   1133   1.4   garbled 			else if (c < 0x66)
   1134   1.4   garbled 				powerp->battery_state = APM_BATT_HIGH;
   1135   1.4   garbled 			else
   1136   1.4   garbled 				powerp->battery_state = APM_BATT_UNKNOWN;
   1137   1.4   garbled 		}
   1138  1.31  macallan 
   1139  1.31  macallan 		if (sc->sc_ext_status & TS102_EXT_STATUS_MAIN_POWER_AVAILABLE)
   1140   1.4   garbled 			powerp->ac_state = APM_AC_ON;
   1141   1.4   garbled 		else
   1142   1.4   garbled 			powerp->ac_state = APM_AC_OFF;
   1143   1.4   garbled 		break;
   1144   1.4   garbled 
   1145   1.4   garbled 	case APM_IOC_NEXTEVENT:
   1146   1.4   garbled 		if (!sc->sc_event_count)
   1147   1.4   garbled 			return EAGAIN;
   1148   1.4   garbled 
   1149   1.4   garbled 		evp = (struct apm_event_info *)data;
   1150   1.4   garbled 		i = sc->sc_event_ptr + APM_NEVENTS - sc->sc_event_count;
   1151   1.4   garbled 		i %= APM_NEVENTS;
   1152   1.4   garbled 		*evp = sc->sc_event_list[i];
   1153   1.4   garbled 		sc->sc_event_count--;
   1154   1.4   garbled 		return(0);
   1155   1.4   garbled 
   1156   1.4   garbled 	/* this ioctl assumes the caller knows exactly what he is doing */
   1157   1.4   garbled 	case TCTRL_CMD_REQ:
   1158   1.4   garbled 		reqn = (struct tctrl_req *)data;
   1159  1.35        ad 		if ((i = kauth_authorize_generic(l->l_cred,
   1160  1.36      elad 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0 &&
   1161   1.4   garbled 		    (reqn->cmdbuf[0] == TS102_OP_CTL_BITPORT ||
   1162   1.4   garbled 		    (reqn->cmdbuf[0] >= TS102_OP_CTL_WATCHDOG &&
   1163   1.4   garbled 		    reqn->cmdbuf[0] <= TS102_OP_CTL_SECURITY_KEY) ||
   1164   1.4   garbled 		    reqn->cmdbuf[0] == TS102_OP_CTL_TIMEZONE ||
   1165   1.4   garbled 		    reqn->cmdbuf[0] == TS102_OP_CTL_DIAGNOSTIC_MODE ||
   1166   1.4   garbled 		    reqn->cmdbuf[0] == TS102_OP_CMD_SOFTWARE_RESET ||
   1167   1.4   garbled 		    (reqn->cmdbuf[0] >= TS102_OP_CMD_SET_RTC &&
   1168   1.4   garbled 		    reqn->cmdbuf[0] < TS102_OP_RD_INT_CHARGE_LEVEL) ||
   1169   1.4   garbled 		    reqn->cmdbuf[0] > TS102_OP_RD_EXT_CHARGE_LEVEL))
   1170   1.4   garbled 			return(i);
   1171  1.38  macallan 		tadpole_request(reqn, 0, l->l_proc ? 1 : 0);
   1172   1.4   garbled 		break;
   1173   1.7       jdc 	/* serial power mode (via auxiotwo) */
   1174   1.7       jdc 	case TCTRL_SERIAL_PWR:
   1175  1.28       uwe 		pwrreq = (struct tctrl_pwr *)data;
   1176   1.7       jdc 		if (pwrreq->rw)
   1177   1.7       jdc 			pwrreq->state = auxiotwoserialgetapm();
   1178   1.7       jdc 		else
   1179   1.7       jdc 			auxiotwoserialsetapm(pwrreq->state);
   1180   1.7       jdc 		break;
   1181   1.7       jdc 
   1182   1.7       jdc 	/* modem power mode (via auxio) */
   1183   1.7       jdc 	case TCTRL_MODEM_PWR:
   1184   1.7       jdc 		return(EOPNOTSUPP); /* for now */
   1185   1.7       jdc 		break;
   1186   1.4   garbled 
   1187   1.4   garbled 
   1188   1.4   garbled         default:
   1189   1.4   garbled                 return (ENOTTY);
   1190   1.4   garbled         }
   1191   1.4   garbled         return (0);
   1192   1.4   garbled }
   1193   1.4   garbled 
   1194   1.4   garbled int
   1195  1.29  christos tctrlpoll(dev_t dev, int events, struct lwp *l)
   1196   1.4   garbled {
   1197   1.4   garbled 	struct tctrl_softc *sc = tctrl_cd.cd_devs[TCTRL_STD_DEV];
   1198   1.4   garbled 	int revents = 0;
   1199   1.4   garbled 
   1200   1.4   garbled 	if (events & (POLLIN | POLLRDNORM)) {
   1201   1.4   garbled 		if (sc->sc_event_count)
   1202   1.4   garbled 			revents |= events & (POLLIN | POLLRDNORM);
   1203   1.4   garbled 		else
   1204  1.29  christos 			selrecord(l, &sc->sc_rsel);
   1205   1.4   garbled 	}
   1206   1.4   garbled 
   1207   1.4   garbled 	return (revents);
   1208   1.1      matt }
   1209  1.20  jdolecek 
   1210  1.20  jdolecek static void
   1211  1.20  jdolecek filt_tctrlrdetach(struct knote *kn)
   1212  1.20  jdolecek {
   1213  1.20  jdolecek 	struct tctrl_softc *sc = kn->kn_hook;
   1214  1.20  jdolecek 	int s;
   1215  1.20  jdolecek 
   1216  1.20  jdolecek 	s = splts102();
   1217  1.21  christos 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
   1218  1.20  jdolecek 	splx(s);
   1219  1.20  jdolecek }
   1220  1.20  jdolecek 
   1221  1.20  jdolecek static int
   1222  1.20  jdolecek filt_tctrlread(struct knote *kn, long hint)
   1223  1.20  jdolecek {
   1224  1.20  jdolecek 	struct tctrl_softc *sc = kn->kn_hook;
   1225  1.20  jdolecek 
   1226  1.20  jdolecek 	kn->kn_data = sc->sc_event_count;
   1227  1.20  jdolecek 	return (kn->kn_data > 0);
   1228  1.20  jdolecek }
   1229  1.20  jdolecek 
   1230  1.20  jdolecek static const struct filterops tctrlread_filtops =
   1231  1.20  jdolecek 	{ 1, NULL, filt_tctrlrdetach, filt_tctrlread };
   1232  1.20  jdolecek 
   1233  1.20  jdolecek int
   1234  1.20  jdolecek tctrlkqfilter(dev_t dev, struct knote *kn)
   1235  1.20  jdolecek {
   1236  1.20  jdolecek 	struct tctrl_softc *sc = tctrl_cd.cd_devs[TCTRL_STD_DEV];
   1237  1.20  jdolecek 	struct klist *klist;
   1238  1.20  jdolecek 	int s;
   1239  1.20  jdolecek 
   1240  1.20  jdolecek 	switch (kn->kn_filter) {
   1241  1.20  jdolecek 	case EVFILT_READ:
   1242  1.21  christos 		klist = &sc->sc_rsel.sel_klist;
   1243  1.20  jdolecek 		kn->kn_fop = &tctrlread_filtops;
   1244  1.20  jdolecek 		break;
   1245  1.20  jdolecek 
   1246  1.20  jdolecek 	default:
   1247  1.20  jdolecek 		return (1);
   1248  1.20  jdolecek 	}
   1249  1.20  jdolecek 
   1250  1.20  jdolecek 	kn->kn_hook = sc;
   1251  1.20  jdolecek 
   1252  1.20  jdolecek 	s = splts102();
   1253  1.20  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1254  1.20  jdolecek 	splx(s);
   1255  1.20  jdolecek 
   1256  1.20  jdolecek 	return (0);
   1257  1.20  jdolecek }
   1258  1.20  jdolecek 
   1259  1.27  macallan static void
   1260  1.27  macallan tctrl_sensor_setup(struct tctrl_softc *sc)
   1261  1.27  macallan {
   1262  1.27  macallan 	int error;
   1263  1.28       uwe 
   1264  1.27  macallan 	/* case temperature */
   1265  1.39   xtraeme 	(void)strlcpy(sc->sc_sensor[0].desc, "Case temperature",
   1266  1.39   xtraeme 	    sizeof(sc->sc_sensor[0].desc));
   1267  1.39   xtraeme 	sc->sc_sensor[0].sensor = 0;
   1268  1.39   xtraeme 	sc->sc_sensor[0].units = ENVSYS_STEMP;
   1269  1.39   xtraeme 	sc->sc_sensor[0].state = ENVSYS_SVALID;
   1270  1.28       uwe 
   1271  1.27  macallan 	/* battery voltage */
   1272  1.39   xtraeme 	(void)strlcpy(sc->sc_sensor[1].desc, "Internal battery voltage",
   1273  1.39   xtraeme 	    sizeof(sc->sc_sensor[1].desc));
   1274  1.39   xtraeme 	sc->sc_sensor[1].sensor = 1;
   1275  1.39   xtraeme 	sc->sc_sensor[1].units = ENVSYS_SVOLTS_DC;
   1276  1.39   xtraeme 	sc->sc_sensor[1].state = ENVSYS_SVALID;
   1277  1.28       uwe 
   1278  1.27  macallan 	/* DC voltage */
   1279  1.39   xtraeme 	(void)strlcpy(sc->sc_sensor[2].desc, "DC-In voltage",
   1280  1.39   xtraeme 	    sizeof(sc->sc_sensor[2].desc));
   1281  1.39   xtraeme 	sc->sc_sensor[2].sensor = 2;
   1282  1.39   xtraeme 	sc->sc_sensor[2].units = ENVSYS_SVOLTS_DC;
   1283  1.39   xtraeme 	sc->sc_sensor[2].state = ENVSYS_SVALID;
   1284  1.28       uwe 
   1285  1.39   xtraeme 	sc->sc_sme.sme_name = sc->sc_dev.dv_xname;
   1286  1.27  macallan 	sc->sc_sme.sme_nsensors = ENVSYS_NUMSENSORS;
   1287  1.39   xtraeme 	sc->sc_sme.sme_sensor_data = sc->sc_sensor;
   1288  1.27  macallan 	sc->sc_sme.sme_cookie = sc;
   1289  1.27  macallan 	sc->sc_sme.sme_gtredata = tctrl_gtredata;
   1290  1.28       uwe 
   1291  1.27  macallan 	if ((error = sysmon_envsys_register(&sc->sc_sme)) != 0) {
   1292  1.28       uwe 		printf("%s: couldn't register sensors (%d)\n",
   1293  1.27  macallan 		    sc->sc_dev.dv_xname, error);
   1294  1.27  macallan 	}
   1295  1.28       uwe 
   1296  1.27  macallan 	/* now register the power button */
   1297  1.28       uwe 
   1298  1.27  macallan 	sysmon_task_queue_init();
   1299  1.27  macallan 
   1300  1.27  macallan 	sc->sc_powerpressed = 0;
   1301  1.31  macallan 	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
   1302  1.31  macallan 	sc->sc_sm_pbutton.smpsw_name = sc->sc_dev.dv_xname;
   1303  1.31  macallan 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
   1304  1.31  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
   1305  1.28       uwe 		printf("%s: unable to register power button with sysmon\n",
   1306  1.27  macallan 		    sc->sc_dev.dv_xname);
   1307  1.31  macallan 
   1308  1.31  macallan 	memset(&sc->sc_sm_lid, 0, sizeof(struct sysmon_pswitch));
   1309  1.31  macallan 	sc->sc_sm_lid.smpsw_name = sc->sc_dev.dv_xname;
   1310  1.31  macallan 	sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
   1311  1.31  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
   1312  1.31  macallan 		printf("%s: unable to register lid switch with sysmon\n",
   1313  1.31  macallan 		    sc->sc_dev.dv_xname);
   1314  1.31  macallan 
   1315  1.31  macallan 	memset(&sc->sc_sm_ac, 0, sizeof(struct sysmon_pswitch));
   1316  1.31  macallan 	sc->sc_sm_ac.smpsw_name = sc->sc_dev.dv_xname;
   1317  1.31  macallan 	sc->sc_sm_ac.smpsw_type = PSWITCH_TYPE_ACADAPTER;
   1318  1.31  macallan 	if (sysmon_pswitch_register(&sc->sc_sm_ac) != 0)
   1319  1.31  macallan 		printf("%s: unable to register AC adaptor with sysmon\n",
   1320  1.31  macallan 		    sc->sc_dev.dv_xname);
   1321  1.27  macallan }
   1322  1.27  macallan 
   1323  1.27  macallan static void
   1324  1.27  macallan tctrl_power_button_pressed(void *arg)
   1325  1.27  macallan {
   1326  1.27  macallan 	struct tctrl_softc *sc = arg;
   1327  1.27  macallan 
   1328  1.31  macallan 	sysmon_pswitch_event(&sc->sc_sm_pbutton, PSWITCH_EVENT_PRESSED);
   1329  1.27  macallan 	sc->sc_powerpressed = 0;
   1330  1.27  macallan }
   1331  1.27  macallan 
   1332  1.31  macallan static void
   1333  1.31  macallan tctrl_lid_state(struct tctrl_softc *sc)
   1334  1.31  macallan {
   1335  1.31  macallan 	int state;
   1336  1.31  macallan 
   1337  1.31  macallan 	state = (sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN) ?
   1338  1.40   xtraeme 	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED;
   1339  1.31  macallan 	sysmon_pswitch_event(&sc->sc_sm_lid, state);
   1340  1.31  macallan }
   1341  1.31  macallan 
   1342  1.31  macallan static void
   1343  1.31  macallan tctrl_ac_state(struct tctrl_softc *sc)
   1344  1.31  macallan {
   1345  1.31  macallan 	int state;
   1346  1.31  macallan 
   1347  1.31  macallan 	state = (sc->sc_ext_status & TS102_EXT_STATUS_MAIN_POWER_AVAILABLE) ?
   1348  1.40   xtraeme 	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED;
   1349  1.31  macallan 	sysmon_pswitch_event(&sc->sc_sm_ac, state);
   1350  1.31  macallan }
   1351  1.31  macallan 
   1352  1.28       uwe static int
   1353  1.27  macallan tctrl_powerfail(void *arg)
   1354  1.27  macallan {
   1355  1.27  macallan 	struct tctrl_softc *sc = (struct tctrl_softc *)arg;
   1356  1.27  macallan 
   1357  1.27  macallan 	/*
   1358  1.27  macallan 	 * We lost power. Queue a callback with thread context to
   1359  1.27  macallan 	 * handle all the real work.
   1360  1.27  macallan 	 */
   1361  1.27  macallan 	if (sc->sc_powerpressed == 0) {
   1362  1.27  macallan 		sc->sc_powerpressed = 1;
   1363  1.27  macallan 		sysmon_task_queue_sched(0, tctrl_power_button_pressed, sc);
   1364  1.27  macallan 	}
   1365  1.27  macallan 	return (1);
   1366  1.27  macallan }
   1367  1.27  macallan 
   1368  1.27  macallan static int
   1369  1.39   xtraeme tctrl_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
   1370  1.27  macallan {
   1371  1.27  macallan 	/*struct tctrl_softc *sc = sme->sme_cookie;*/
   1372  1.27  macallan 	struct tctrl_req req;
   1373  1.38  macallan 	int sleepable;
   1374  1.27  macallan 	int i;
   1375  1.28       uwe 
   1376  1.39   xtraeme 	i = edata->sensor;
   1377  1.38  macallan 	sleepable = curlwp ? 1 : 0;
   1378  1.28       uwe 
   1379  1.27  macallan 	switch (i)
   1380  1.27  macallan 	{
   1381  1.27  macallan 		case 0:	/* case temperature */
   1382  1.27  macallan 			req.cmdbuf[0] = TS102_OP_RD_CURRENT_TEMP;
   1383  1.27  macallan 			req.cmdlen = 1;
   1384  1.27  macallan 			req.rsplen = 2;
   1385  1.38  macallan 			tadpole_request(&req, 0, sleepable);
   1386  1.39   xtraeme 			edata->value_cur =             /* 273160? */
   1387  1.28       uwe 			    (uint32_t)((int)((int)req.rspbuf[0] - 32) * 5000000
   1388  1.27  macallan 			    / 9 + 273150000);
   1389  1.39   xtraeme 			cur_tre->state = ENVSYS_SVALID;
   1390  1.27  macallan 			req.cmdbuf[0] = TS102_OP_RD_MAX_TEMP;
   1391  1.27  macallan 			req.cmdlen = 1;
   1392  1.27  macallan 			req.rsplen = 2;
   1393  1.38  macallan 			tadpole_request(&req, 0, sleepable);
   1394  1.39   xtraeme 			edata->value_max =
   1395  1.28       uwe 			    (uint32_t)((int)((int)req.rspbuf[0] - 32) * 5000000
   1396  1.27  macallan 			    / 9 + 273150000);
   1397  1.39   xtraeme 			edata->flags |= ENVSYS_FVALID_MAX;
   1398  1.27  macallan 			req.cmdbuf[0] = TS102_OP_RD_MIN_TEMP;
   1399  1.27  macallan 			req.cmdlen = 1;
   1400  1.27  macallan 			req.rsplen = 2;
   1401  1.38  macallan 			tadpole_request(&req, 0, sleepable);
   1402  1.39   xtraeme 			edata->value_min =
   1403  1.28       uwe 			    (uint32_t)((int)((int)req.rspbuf[0] - 32) * 5000000
   1404  1.27  macallan 			    / 9 + 273150000);
   1405  1.39   xtraeme 			edata->flags |= ENVSYS_FVALID_MIN;
   1406  1.39   xtraeme 			edata->units = ENVSYS_STEMP;
   1407  1.27  macallan 			break;
   1408  1.28       uwe 
   1409  1.27  macallan 		case 1: /* battery voltage */
   1410  1.27  macallan 			{
   1411  1.39   xtraeme 				edata->state = ENVSYS_SVALID;
   1412  1.39   xtraeme 				edata->units = ENVSYS_SVOLTS_DC;
   1413  1.27  macallan 				req.cmdbuf[0] = TS102_OP_RD_INT_BATT_VLT;
   1414  1.27  macallan 				req.cmdlen = 1;
   1415  1.27  macallan 				req.rsplen = 2;
   1416  1.38  macallan 				tadpole_request(&req, 0, sleepable);
   1417  1.39   xtraeme 				edata->value_cur = (int32_t)req.rspbuf[0] *
   1418  1.27  macallan 				    1000000 / 11;
   1419  1.27  macallan 			}
   1420  1.27  macallan 			break;
   1421  1.27  macallan 		case 2: /* DC voltage */
   1422  1.27  macallan 			{
   1423  1.39   xtraeme 				edata->state = ENVSYS_SVALID;
   1424  1.39   xtraeme 				edata->units = ENVSYS_SVOLTS_DC;
   1425  1.27  macallan 				req.cmdbuf[0] = TS102_OP_RD_DC_IN_VLT;
   1426  1.27  macallan 				req.cmdlen = 1;
   1427  1.27  macallan 				req.rsplen = 2;
   1428  1.38  macallan 				tadpole_request(&req, 0, sleepable);
   1429  1.39   xtraeme 				edata->value_cur = (int32_t)req.rspbuf[0] *
   1430  1.27  macallan 				    1000000 / 11;
   1431  1.27  macallan 			}
   1432  1.27  macallan 			break;
   1433  1.27  macallan 	}
   1434  1.39   xtraeme 	edata->state = ENVSYS_SVALID;
   1435  1.27  macallan 	return 0;
   1436  1.27  macallan }
   1437  1.27  macallan 
   1438  1.30  macallan static void
   1439  1.30  macallan tctrl_create_event_thread(void *v)
   1440  1.30  macallan {
   1441  1.30  macallan 	struct tctrl_softc *sc = v;
   1442  1.30  macallan 	const char *name = sc->sc_dev.dv_xname;
   1443  1.30  macallan 
   1444  1.30  macallan 	if (kthread_create1(tctrl_event_thread, sc, &sc->sc_thread, "%s",
   1445  1.30  macallan 	    name) != 0) {
   1446  1.30  macallan 		printf("%s: unable to create event kthread", name);
   1447  1.30  macallan 	}
   1448  1.30  macallan }
   1449  1.30  macallan 
   1450  1.30  macallan static void
   1451  1.30  macallan tctrl_event_thread(void *v)
   1452  1.30  macallan {
   1453  1.30  macallan 	struct tctrl_softc *sc = v;
   1454  1.30  macallan 	struct device *dv;
   1455  1.30  macallan 	struct sd_softc *sd = NULL;
   1456  1.30  macallan 	struct lance_softc *le = NULL;
   1457  1.30  macallan 	int ticks = hz/2;
   1458  1.30  macallan 	int rcount, wcount;
   1459  1.30  macallan 	int s;
   1460  1.30  macallan 
   1461  1.30  macallan 	while (sd == NULL) {
   1462  1.30  macallan 		for (dv = alldevs.tqh_first; dv; dv = dv->dv_list.tqe_next) {
   1463  1.30  macallan 			if (strcmp(dv->dv_xname, "sd0") == 0) {
   1464  1.30  macallan 			    	sd = (struct sd_softc *)dv;
   1465  1.30  macallan 			}
   1466  1.30  macallan 			if (le == NULL) {
   1467  1.30  macallan 				if (strcmp(dv->dv_xname, "le0") == 0)
   1468  1.30  macallan 					le = (struct lance_softc *)dv;
   1469  1.30  macallan 			}
   1470  1.30  macallan 		}
   1471  1.30  macallan 		if (sd == NULL)
   1472  1.30  macallan 			tsleep(&sc->sc_events, PWAIT, "probe_disk", hz);
   1473  1.30  macallan 	}
   1474  1.30  macallan 	printf("found %s\n", sd->sc_dev.dv_xname);
   1475  1.33     blymn 	rcount = sd->sc_dk.dk_stats->io_rxfer;
   1476  1.33     blymn 	wcount = sd->sc_dk.dk_stats->io_wxfer;
   1477  1.30  macallan 
   1478  1.30  macallan 	tctrl_read_event_status(sc);
   1479  1.30  macallan 
   1480  1.30  macallan 	while (1) {
   1481  1.30  macallan 		tsleep(&sc->sc_events, PWAIT, "tctrl_event", ticks);
   1482  1.30  macallan 		s = splhigh();
   1483  1.33     blymn 		if ((rcount != sd->sc_dk.dk_stats->io_rxfer) ||
   1484  1.33     blymn 		    (wcount != sd->sc_dk.dk_stats->io_wxfer)) {
   1485  1.33     blymn 			rcount = sd->sc_dk.dk_stats->io_rxfer;
   1486  1.33     blymn 			wcount = sd->sc_dk.dk_stats->io_wxfer;
   1487  1.30  macallan 			sc->sc_lcdwanted |= TS102_LCD_DISK_ACTIVE;
   1488  1.30  macallan 		} else
   1489  1.30  macallan 			sc->sc_lcdwanted &= ~TS102_LCD_DISK_ACTIVE;
   1490  1.30  macallan 		if (le != NULL) {
   1491  1.30  macallan 			if (le->sc_havecarrier != 0) {
   1492  1.30  macallan 				sc->sc_lcdwanted |= TS102_LCD_LAN_ACTIVE;
   1493  1.30  macallan 			} else
   1494  1.30  macallan 				sc->sc_lcdwanted &= ~TS102_LCD_LAN_ACTIVE;
   1495  1.30  macallan 		}
   1496  1.30  macallan 		splx(s);
   1497  1.30  macallan 		tctrl_update_lcd(sc);
   1498  1.30  macallan 		if (sc->sc_ext_pending)
   1499  1.30  macallan 			tctrl_read_event_status(sc);
   1500  1.30  macallan 	}
   1501  1.30  macallan }
   1502  1.30  macallan 
   1503  1.30  macallan void
   1504  1.30  macallan tadpole_register_callback(void (*callback)(void *, int), void *cookie)
   1505  1.30  macallan {
   1506  1.30  macallan 	struct tctrl_softc *sc;
   1507  1.30  macallan 
   1508  1.30  macallan 	sc = (struct tctrl_softc *) tctrl_cd.cd_devs[TCTRL_STD_DEV];
   1509  1.30  macallan 	sc->sc_video_callback = callback;
   1510  1.30  macallan 	sc->sc_video_callback_cookie = cookie;
   1511  1.30  macallan 	if (sc->sc_video_callback != NULL) {
   1512  1.30  macallan 		sc->sc_video_callback(sc->sc_video_callback_cookie,
   1513  1.30  macallan 		    sc->sc_extvga);
   1514  1.30  macallan 	}
   1515  1.30  macallan }
   1516