Home | History | Annotate | Line # | Download | only in pnpbios
pckbc_pnpbios.c revision 1.2
      1  1.2  minoura /*	$NetBSD: pckbc_pnpbios.c,v 1.2 2001/03/31 09:20:40 minoura Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej /*
     40  1.1  thorpej  * PNPBIOS attachment for the PC Keyboard Controller driver.
     41  1.1  thorpej  *
     42  1.1  thorpej  * This is a little wonky.  The keyboard controller actually
     43  1.1  thorpej  * has 2 PNPBIOS nodes: one for the controller and the keyboard
     44  1.1  thorpej  * interrupt, and one for the aux port (mouse) interrupt.
     45  1.1  thorpej  *
     46  1.1  thorpej  * For this reason, we actually attach *two* instances of this
     47  1.1  thorpej  * driver.  After both of them have been found, then we attach
     48  1.1  thorpej  * sub-devices.
     49  1.1  thorpej  */
     50  1.1  thorpej 
     51  1.1  thorpej #include <sys/param.h>
     52  1.1  thorpej #include <sys/systm.h>
     53  1.1  thorpej #include <sys/kernel.h>
     54  1.1  thorpej #include <sys/proc.h>
     55  1.1  thorpej #include <sys/device.h>
     56  1.1  thorpej #include <sys/malloc.h>
     57  1.1  thorpej #include <sys/errno.h>
     58  1.1  thorpej #include <sys/queue.h>
     59  1.1  thorpej #include <sys/lock.h>
     60  1.1  thorpej 
     61  1.1  thorpej #include <machine/bus.h>
     62  1.1  thorpej 
     63  1.1  thorpej #include <dev/isa/isareg.h>
     64  1.1  thorpej #include <dev/isa/isavar.h>
     65  1.1  thorpej 
     66  1.1  thorpej #include <dev/ic/i8042reg.h>
     67  1.1  thorpej #include <dev/ic/pckbcvar.h>
     68  1.1  thorpej 
     69  1.1  thorpej #include <i386/pnpbios/pnpbiosvar.h>
     70  1.1  thorpej 
     71  1.1  thorpej int	pckbc_pnpbios_match(struct device *, struct cfdata *, void *);
     72  1.1  thorpej void	pckbc_pnpbios_attach(struct device *, struct device *, void *);
     73  1.1  thorpej 
     74  1.1  thorpej struct pckbc_pnpbios_softc {
     75  1.1  thorpej 	struct pckbc_softc sc_pckbc;
     76  1.1  thorpej 
     77  1.1  thorpej 	int sc_irq;
     78  1.1  thorpej 	int sc_ist;
     79  1.1  thorpej 	pckbc_slot_t sc_slot;
     80  1.1  thorpej 
     81  1.1  thorpej 	/* Only on keyboard port... */
     82  1.1  thorpej 	void *sc_ih;
     83  1.1  thorpej 	struct pckbc_pnpbios_softc *sc_aux_port;
     84  1.1  thorpej };
     85  1.1  thorpej 
     86  1.1  thorpej extern struct cfdriver pckbc_cd;
     87  1.1  thorpej 
     88  1.1  thorpej struct cfattach pckbc_pnpbios_ca = {
     89  1.1  thorpej 	sizeof(struct pckbc_pnpbios_softc), pckbc_pnpbios_match,
     90  1.1  thorpej 	    pckbc_pnpbios_attach,
     91  1.1  thorpej };
     92  1.1  thorpej 
     93  1.1  thorpej void	pckbc_pnpbios_intr_establish(struct pckbc_softc *, pckbc_slot_t);
     94  1.1  thorpej 
     95  1.1  thorpej int
     96  1.1  thorpej pckbc_pnpbios_match(struct device *parent,
     97  1.1  thorpej     struct cfdata *match,
     98  1.1  thorpej     void *aux)
     99  1.1  thorpej {
    100  1.1  thorpej 	struct pnpbiosdev_attach_args *aa = aux;
    101  1.1  thorpej 
    102  1.2  minoura 	if (strcmp(aa->idstr, "PNP0303") == 0 ||
    103  1.2  minoura 	    strcmp(aa->idstr, "PNP0320") == 0)	/* Japanese 106 */
    104  1.2  minoura 		return (1);			/* plus more? */
    105  1.1  thorpej 	if (strcmp(aa->idstr, "PNP0F13") == 0)
    106  1.1  thorpej 		return (1);
    107  1.1  thorpej 
    108  1.1  thorpej 	return (0);
    109  1.1  thorpej }
    110  1.1  thorpej 
    111  1.1  thorpej void
    112  1.1  thorpej pckbc_pnpbios_attach(struct device *parent,
    113  1.1  thorpej     struct device *self,
    114  1.1  thorpej     void *aux)
    115  1.1  thorpej {
    116  1.1  thorpej 	struct pckbc_pnpbios_softc *psc = (void *) self, *peer_psc, *kbd_psc;
    117  1.1  thorpej 	struct pckbc_softc *sc = &psc->sc_pckbc;
    118  1.1  thorpej 	struct pckbc_internal *t;
    119  1.1  thorpej 	struct pnpbiosdev_attach_args *aa = aux;
    120  1.1  thorpej 	bus_space_tag_t iot;
    121  1.1  thorpej 	bus_space_handle_t ioh_d, ioh_c;
    122  1.1  thorpej 	pckbc_slot_t peer;
    123  1.1  thorpej 	int iobase, i;
    124  1.1  thorpej 
    125  1.2  minoura 	if (strncmp(aa->idstr, "PNP03", 5) == 0) {
    126  1.1  thorpej 		psc->sc_slot = PCKBC_KBD_SLOT;
    127  1.1  thorpej 		peer = PCKBC_AUX_SLOT;
    128  1.1  thorpej 	} else if (strcmp(aa->idstr, "PNP0F13") == 0) {
    129  1.1  thorpej 		psc->sc_slot = PCKBC_AUX_SLOT;
    130  1.1  thorpej 		peer = PCKBC_KBD_SLOT;
    131  1.1  thorpej 	} else {
    132  1.1  thorpej 		printf(": unknown port!\n");
    133  1.1  thorpej 		panic("pckbc_pnpbios_attach: impossible");
    134  1.1  thorpej 	}
    135  1.1  thorpej 
    136  1.1  thorpej 	printf(": %s port\n", pckbc_slot_names[psc->sc_slot]);
    137  1.1  thorpej 
    138  1.1  thorpej 	if (pnpbios_getirqnum(aa->pbt, aa->resc, 0, &psc->sc_irq,
    139  1.1  thorpej 	    &psc->sc_ist) != 0) {
    140  1.1  thorpej 		printf("%s: unable to get IRQ number or type\n",
    141  1.1  thorpej 		    sc->sc_dv.dv_xname);
    142  1.1  thorpej 		return;
    143  1.1  thorpej 	}
    144  1.1  thorpej 
    145  1.1  thorpej 	if (psc->sc_slot == PCKBC_KBD_SLOT) {
    146  1.1  thorpej 		if (pnpbios_getiobase(aa->pbt, aa->resc, 0, &iot, &iobase)) {
    147  1.1  thorpej 			printf("%s: can't get iobase\n", sc->sc_dv.dv_xname);
    148  1.1  thorpej 			return;
    149  1.1  thorpej 		}
    150  1.1  thorpej 
    151  1.1  thorpej 		sc->intr_establish = pckbc_pnpbios_intr_establish;
    152  1.1  thorpej 
    153  1.1  thorpej 		if (pckbc_is_console(iot, iobase)) {
    154  1.1  thorpej 			t = &pckbc_consdata;
    155  1.1  thorpej 			ioh_d = t->t_ioh_d;
    156  1.1  thorpej 			ioh_c = t->t_ioh_c;
    157  1.1  thorpej 			pckbc_console_attached = 1;
    158  1.1  thorpej 			/* t->t_cmdbyte was initialized by cnattach */
    159  1.1  thorpej 		} else {
    160  1.1  thorpej 			if (pnpbios_io_map(aa->pbt, aa->resc, 0,
    161  1.1  thorpej 					   &iot, &ioh_d) ||
    162  1.1  thorpej 			    pnpbios_io_map(aa->pbt, aa->resc, 1,
    163  1.1  thorpej 			    		   &iot, &ioh_c))
    164  1.1  thorpej 				panic("pckbc_pnpbios_attach: couldn't map");
    165  1.1  thorpej 
    166  1.1  thorpej 			t = malloc(sizeof(struct pckbc_internal),
    167  1.1  thorpej 			    M_DEVBUF, M_WAITOK);
    168  1.1  thorpej 			memset(t, 0, sizeof(*t));
    169  1.1  thorpej 			t->t_iot = iot;
    170  1.1  thorpej 			t->t_ioh_d = ioh_d;
    171  1.1  thorpej 			t->t_ioh_c = ioh_c;
    172  1.1  thorpej 			t->t_addr = iobase;
    173  1.1  thorpej 			t->t_cmdbyte = KC8_CPU;	/* Enable ports */
    174  1.1  thorpej 			callout_init(&t->t_cleanup);
    175  1.1  thorpej 		}
    176  1.1  thorpej 
    177  1.1  thorpej 		t->t_sc = sc;
    178  1.1  thorpej 		sc->id = t;
    179  1.1  thorpej 	}
    180  1.1  thorpej 
    181  1.1  thorpej 	for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
    182  1.1  thorpej 		peer_psc = pckbc_cd.cd_devs[i];
    183  1.1  thorpej 		if (peer_psc != NULL &&
    184  1.1  thorpej 		    peer_psc != psc &&
    185  1.1  thorpej 		    peer_psc->sc_pckbc.sc_dv.dv_parent ==
    186  1.1  thorpej 		     psc->sc_pckbc.sc_dv.dv_parent)
    187  1.1  thorpej 			break;
    188  1.1  thorpej 		peer_psc = NULL;
    189  1.1  thorpej 	}
    190  1.1  thorpej 
    191  1.1  thorpej 	if (peer_psc != NULL) {
    192  1.1  thorpej 		/*
    193  1.1  thorpej 		 * Have both -- finish attaching the one marked "keyboard".
    194  1.1  thorpej 		 */
    195  1.1  thorpej 		if (psc->sc_slot == PCKBC_KBD_SLOT) {
    196  1.1  thorpej 			kbd_psc = psc;
    197  1.1  thorpej 			kbd_psc->sc_aux_port = peer_psc;
    198  1.1  thorpej 		} else {
    199  1.1  thorpej 			kbd_psc = peer_psc;
    200  1.1  thorpej 			kbd_psc->sc_aux_port = psc;
    201  1.1  thorpej 		}
    202  1.1  thorpej 		pckbc_attach(&kbd_psc->sc_pckbc);
    203  1.1  thorpej 	}
    204  1.1  thorpej }
    205  1.1  thorpej 
    206  1.1  thorpej void
    207  1.1  thorpej pckbc_pnpbios_intr_establish(struct pckbc_softc *sc,
    208  1.1  thorpej     pckbc_slot_t slot)
    209  1.1  thorpej {
    210  1.1  thorpej 	struct pckbc_pnpbios_softc *psc = (void *) sc;
    211  1.1  thorpej 	void *rv;
    212  1.1  thorpej 	int irq, ist;
    213  1.1  thorpej 
    214  1.1  thorpej 	/*
    215  1.1  thorpej 	 * Note we're always called with the keyboard slot.
    216  1.1  thorpej 	 */
    217  1.1  thorpej 
    218  1.1  thorpej 	if (slot == PCKBC_KBD_SLOT) {
    219  1.1  thorpej 		irq = psc->sc_irq;
    220  1.1  thorpej 		ist = psc->sc_ist;
    221  1.1  thorpej 	} else {
    222  1.1  thorpej 		irq = psc->sc_aux_port->sc_irq;
    223  1.1  thorpej 		ist = psc->sc_aux_port->sc_ist;
    224  1.1  thorpej 	}
    225  1.1  thorpej 
    226  1.1  thorpej 	rv = isa_intr_establish(0/*XXX*/, irq, ist, IPL_TTY, pckbcintr, sc);
    227  1.1  thorpej 	if (rv == NULL) {
    228  1.1  thorpej 		printf("%s: unable to establish interrupt for %s slot\n",
    229  1.1  thorpej 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    230  1.1  thorpej 	} else {
    231  1.1  thorpej 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
    232  1.1  thorpej 		    irq, pckbc_slot_names[slot]);
    233  1.1  thorpej 	}
    234  1.1  thorpej }
    235