Home | History | Annotate | Line # | Download | only in pnpbios
pckbc_pnpbios.c revision 1.17
      1  1.17  tsutsui /*	$NetBSD: pckbc_pnpbios.c,v 1.17 2008/06/08 18:35:25 tsutsui 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  *
     19   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  thorpej  */
     31   1.1  thorpej 
     32   1.1  thorpej /*
     33   1.1  thorpej  * PNPBIOS attachment for the PC Keyboard Controller driver.
     34   1.1  thorpej  *
     35   1.1  thorpej  * This is a little wonky.  The keyboard controller actually
     36   1.1  thorpej  * has 2 PNPBIOS nodes: one for the controller and the keyboard
     37   1.1  thorpej  * interrupt, and one for the aux port (mouse) interrupt.
     38   1.1  thorpej  *
     39   1.1  thorpej  * For this reason, we actually attach *two* instances of this
     40   1.1  thorpej  * driver.  After both of them have been found, then we attach
     41   1.1  thorpej  * sub-devices.
     42   1.1  thorpej  */
     43   1.3    lukem 
     44   1.3    lukem #include <sys/cdefs.h>
     45  1.17  tsutsui __KERNEL_RCSID(0, "$NetBSD: pckbc_pnpbios.c,v 1.17 2008/06/08 18:35:25 tsutsui Exp $");
     46   1.1  thorpej 
     47   1.1  thorpej #include <sys/param.h>
     48   1.1  thorpej #include <sys/systm.h>
     49   1.1  thorpej #include <sys/kernel.h>
     50   1.1  thorpej #include <sys/proc.h>
     51   1.1  thorpej #include <sys/device.h>
     52   1.1  thorpej #include <sys/malloc.h>
     53   1.1  thorpej #include <sys/errno.h>
     54   1.1  thorpej #include <sys/queue.h>
     55  1.13       ad #include <sys/bus.h>
     56   1.1  thorpej 
     57   1.1  thorpej #include <dev/isa/isareg.h>
     58   1.1  thorpej #include <dev/isa/isavar.h>
     59   1.1  thorpej 
     60   1.1  thorpej #include <dev/ic/i8042reg.h>
     61   1.1  thorpej #include <dev/ic/pckbcvar.h>
     62   1.1  thorpej 
     63   1.1  thorpej #include <i386/pnpbios/pnpbiosvar.h>
     64   1.1  thorpej 
     65  1.14     cube int	pckbc_pnpbios_match(device_t, cfdata_t, void *);
     66  1.14     cube void	pckbc_pnpbios_attach(device_t, device_t, void *);
     67   1.1  thorpej 
     68   1.1  thorpej struct pckbc_pnpbios_softc {
     69   1.1  thorpej 	struct pckbc_softc sc_pckbc;
     70   1.1  thorpej 
     71   1.8     matt 	isa_chipset_tag_t sc_ic;
     72   1.1  thorpej 	int sc_irq;
     73   1.1  thorpej 	int sc_ist;
     74   1.1  thorpej 	pckbc_slot_t sc_slot;
     75   1.4       ws };
     76   1.1  thorpej 
     77   1.4       ws /* Save first port: */
     78   1.4       ws static struct pckbc_pnpbios_softc *first;
     79   1.1  thorpej 
     80   1.1  thorpej extern struct cfdriver pckbc_cd;
     81   1.1  thorpej 
     82  1.14     cube CFATTACH_DECL_NEW(pckbc_pnpbios, sizeof(struct pckbc_pnpbios_softc),
     83   1.7  thorpej     pckbc_pnpbios_match, pckbc_pnpbios_attach, NULL, NULL);
     84   1.1  thorpej 
     85   1.1  thorpej void	pckbc_pnpbios_intr_establish(struct pckbc_softc *, pckbc_slot_t);
     86  1.14     cube static void pckbc_pnpbios_finish_attach(device_t);
     87   1.1  thorpej 
     88   1.1  thorpej int
     89  1.15     cube pckbc_pnpbios_match(device_t parent, cfdata_t match, void *aux)
     90   1.1  thorpej {
     91   1.1  thorpej 	struct pnpbiosdev_attach_args *aa = aux;
     92   1.1  thorpej 
     93   1.2  minoura 	if (strcmp(aa->idstr, "PNP0303") == 0 ||
     94   1.2  minoura 	    strcmp(aa->idstr, "PNP0320") == 0)	/* Japanese 106 */
     95   1.2  minoura 		return (1);			/* plus more? */
     96   1.1  thorpej 	if (strcmp(aa->idstr, "PNP0F13") == 0)
     97   1.1  thorpej 		return (1);
     98   1.1  thorpej 
     99   1.1  thorpej 	return (0);
    100   1.1  thorpej }
    101   1.1  thorpej 
    102   1.1  thorpej void
    103  1.14     cube pckbc_pnpbios_attach(device_t parent, device_t self, void *aux)
    104   1.1  thorpej {
    105  1.14     cube 	struct pckbc_pnpbios_softc *psc = device_private(self);
    106   1.1  thorpej 	struct pckbc_softc *sc = &psc->sc_pckbc;
    107   1.1  thorpej 	struct pckbc_internal *t;
    108   1.1  thorpej 	struct pnpbiosdev_attach_args *aa = aux;
    109   1.1  thorpej 	bus_space_tag_t iot;
    110   1.1  thorpej 	bus_space_handle_t ioh_d, ioh_c;
    111   1.1  thorpej 	pckbc_slot_t peer;
    112   1.4       ws 	int iobase;
    113   1.1  thorpej 
    114  1.14     cube 	sc->sc_dv = self;
    115   1.2  minoura 	if (strncmp(aa->idstr, "PNP03", 5) == 0) {
    116   1.1  thorpej 		psc->sc_slot = PCKBC_KBD_SLOT;
    117   1.1  thorpej 		peer = PCKBC_AUX_SLOT;
    118   1.1  thorpej 	} else if (strcmp(aa->idstr, "PNP0F13") == 0) {
    119   1.1  thorpej 		psc->sc_slot = PCKBC_AUX_SLOT;
    120   1.1  thorpej 		peer = PCKBC_KBD_SLOT;
    121   1.1  thorpej 	} else {
    122  1.14     cube 		aprint_error(": unknown port!\n");
    123   1.1  thorpej 		panic("pckbc_pnpbios_attach: impossible");
    124   1.1  thorpej 	}
    125   1.1  thorpej 
    126  1.14     cube 	aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
    127   1.1  thorpej 
    128   1.1  thorpej 	if (pnpbios_getirqnum(aa->pbt, aa->resc, 0, &psc->sc_irq,
    129   1.1  thorpej 	    &psc->sc_ist) != 0) {
    130  1.14     cube 		aprint_error_dev(self, "unable to get IRQ number or type\n");
    131   1.1  thorpej 		return;
    132   1.1  thorpej 	}
    133   1.1  thorpej 
    134   1.8     matt 	psc->sc_ic = aa->ic;
    135   1.8     matt 
    136   1.4       ws 	if (!first)
    137   1.4       ws 		first = psc;
    138   1.4       ws 
    139   1.4       ws 	if (!first->sc_pckbc.id) {
    140   1.4       ws 
    141   1.4       ws 		if (pnpbios_getiobase(aa->pbt, aa->resc, 0, &iot, &iobase))
    142   1.1  thorpej 			return;
    143   1.1  thorpej 
    144   1.1  thorpej 		if (pckbc_is_console(iot, iobase)) {
    145   1.1  thorpej 			t = &pckbc_consdata;
    146   1.1  thorpej 			ioh_d = t->t_ioh_d;
    147   1.1  thorpej 			ioh_c = t->t_ioh_c;
    148   1.1  thorpej 			pckbc_console_attached = 1;
    149   1.1  thorpej 			/* t->t_cmdbyte was initialized by cnattach */
    150   1.1  thorpej 		} else {
    151   1.1  thorpej 			if (pnpbios_io_map(aa->pbt, aa->resc, 0,
    152   1.1  thorpej 					   &iot, &ioh_d) ||
    153   1.1  thorpej 			    pnpbios_io_map(aa->pbt, aa->resc, 1,
    154   1.1  thorpej 			    		   &iot, &ioh_c))
    155   1.1  thorpej 				panic("pckbc_pnpbios_attach: couldn't map");
    156   1.1  thorpej 
    157   1.1  thorpej 			t = malloc(sizeof(struct pckbc_internal),
    158   1.1  thorpej 			    M_DEVBUF, M_WAITOK);
    159   1.1  thorpej 			memset(t, 0, sizeof(*t));
    160   1.1  thorpej 			t->t_iot = iot;
    161   1.1  thorpej 			t->t_ioh_d = ioh_d;
    162   1.1  thorpej 			t->t_ioh_c = ioh_c;
    163   1.1  thorpej 			t->t_addr = iobase;
    164   1.1  thorpej 			t->t_cmdbyte = KC8_CPU;	/* Enable ports */
    165  1.11       ad 			callout_init(&t->t_cleanup, 0);
    166   1.1  thorpej 		}
    167   1.1  thorpej 
    168   1.4       ws 		t->t_sc = &first->sc_pckbc;
    169   1.4       ws 		first->sc_pckbc.id = t;
    170   1.1  thorpej 
    171   1.4       ws 		first->sc_pckbc.intr_establish = pckbc_pnpbios_intr_establish;
    172  1.14     cube 		config_defer(first->sc_pckbc.sc_dv,
    173  1.14     cube 			     pckbc_pnpbios_finish_attach);
    174   1.1  thorpej 	}
    175   1.1  thorpej }
    176   1.1  thorpej 
    177  1.14     cube static void
    178  1.14     cube pckbc_pnpbios_finish_attach(device_t dv)
    179  1.14     cube {
    180  1.14     cube 
    181  1.14     cube 	pckbc_attach(device_private(dv));
    182  1.14     cube }
    183  1.14     cube 
    184   1.1  thorpej void
    185   1.1  thorpej pckbc_pnpbios_intr_establish(struct pckbc_softc *sc,
    186   1.1  thorpej     pckbc_slot_t slot)
    187   1.1  thorpej {
    188   1.4       ws 	struct pckbc_pnpbios_softc *psc;
    189   1.8     matt 	isa_chipset_tag_t ic = NULL;
    190   1.4       ws 	void *rv = NULL;
    191   1.1  thorpej 	int irq, ist;
    192   1.4       ws 	int i;
    193   1.1  thorpej 
    194   1.1  thorpej 	/*
    195   1.4       ws 	 * Note we're always called with sc == first.
    196   1.1  thorpej 	 */
    197   1.4       ws 	for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
    198  1.17  tsutsui 		psc = device_lookup_private(&pckbc_cd, i);
    199   1.4       ws 		if (psc && psc->sc_slot == slot) {
    200   1.4       ws 			irq = psc->sc_irq;
    201   1.4       ws 			ist = psc->sc_ist;
    202   1.8     matt 			ic = psc->sc_ic;
    203   1.4       ws 			break;
    204   1.4       ws 		}
    205   1.1  thorpej 	}
    206   1.4       ws 	if (i < pckbc_cd.cd_ndevs)
    207   1.8     matt 		rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
    208   1.1  thorpej 	if (rv == NULL) {
    209  1.15     cube 		aprint_error_dev(sc->sc_dv,
    210  1.15     cube 		    "unable to establish interrupt for %s slot\n",
    211  1.15     cube 		    pckbc_slot_names[slot]);
    212   1.1  thorpej 	} else {
    213  1.15     cube 		aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
    214   1.1  thorpej 		    irq, pckbc_slot_names[slot]);
    215   1.1  thorpej 	}
    216   1.1  thorpej }
    217