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