pckbc_pnpbios.c revision 1.3 1 /* $NetBSD: pckbc_pnpbios.c,v 1.3 2001/11/15 07:03:35 lukem 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/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: pckbc_pnpbios.c,v 1.3 2001/11/15 07:03:35 lukem Exp $");
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/proc.h>
58 #include <sys/device.h>
59 #include <sys/malloc.h>
60 #include <sys/errno.h>
61 #include <sys/queue.h>
62 #include <sys/lock.h>
63
64 #include <machine/bus.h>
65
66 #include <dev/isa/isareg.h>
67 #include <dev/isa/isavar.h>
68
69 #include <dev/ic/i8042reg.h>
70 #include <dev/ic/pckbcvar.h>
71
72 #include <i386/pnpbios/pnpbiosvar.h>
73
74 int pckbc_pnpbios_match(struct device *, struct cfdata *, void *);
75 void pckbc_pnpbios_attach(struct device *, struct device *, void *);
76
77 struct pckbc_pnpbios_softc {
78 struct pckbc_softc sc_pckbc;
79
80 int sc_irq;
81 int sc_ist;
82 pckbc_slot_t sc_slot;
83
84 /* Only on keyboard port... */
85 void *sc_ih;
86 struct pckbc_pnpbios_softc *sc_aux_port;
87 };
88
89 extern struct cfdriver pckbc_cd;
90
91 struct cfattach pckbc_pnpbios_ca = {
92 sizeof(struct pckbc_pnpbios_softc), pckbc_pnpbios_match,
93 pckbc_pnpbios_attach,
94 };
95
96 void pckbc_pnpbios_intr_establish(struct pckbc_softc *, pckbc_slot_t);
97
98 int
99 pckbc_pnpbios_match(struct device *parent,
100 struct cfdata *match,
101 void *aux)
102 {
103 struct pnpbiosdev_attach_args *aa = aux;
104
105 if (strcmp(aa->idstr, "PNP0303") == 0 ||
106 strcmp(aa->idstr, "PNP0320") == 0) /* Japanese 106 */
107 return (1); /* plus more? */
108 if (strcmp(aa->idstr, "PNP0F13") == 0)
109 return (1);
110
111 return (0);
112 }
113
114 void
115 pckbc_pnpbios_attach(struct device *parent,
116 struct device *self,
117 void *aux)
118 {
119 struct pckbc_pnpbios_softc *psc = (void *) self, *peer_psc, *kbd_psc;
120 struct pckbc_softc *sc = &psc->sc_pckbc;
121 struct pckbc_internal *t;
122 struct pnpbiosdev_attach_args *aa = aux;
123 bus_space_tag_t iot;
124 bus_space_handle_t ioh_d, ioh_c;
125 pckbc_slot_t peer;
126 int iobase, i;
127
128 if (strncmp(aa->idstr, "PNP03", 5) == 0) {
129 psc->sc_slot = PCKBC_KBD_SLOT;
130 peer = PCKBC_AUX_SLOT;
131 } else if (strcmp(aa->idstr, "PNP0F13") == 0) {
132 psc->sc_slot = PCKBC_AUX_SLOT;
133 peer = PCKBC_KBD_SLOT;
134 } else {
135 printf(": unknown port!\n");
136 panic("pckbc_pnpbios_attach: impossible");
137 }
138
139 printf(": %s port\n", pckbc_slot_names[psc->sc_slot]);
140
141 if (pnpbios_getirqnum(aa->pbt, aa->resc, 0, &psc->sc_irq,
142 &psc->sc_ist) != 0) {
143 printf("%s: unable to get IRQ number or type\n",
144 sc->sc_dv.dv_xname);
145 return;
146 }
147
148 if (psc->sc_slot == PCKBC_KBD_SLOT) {
149 if (pnpbios_getiobase(aa->pbt, aa->resc, 0, &iot, &iobase)) {
150 printf("%s: can't get iobase\n", sc->sc_dv.dv_xname);
151 return;
152 }
153
154 sc->intr_establish = pckbc_pnpbios_intr_establish;
155
156 if (pckbc_is_console(iot, iobase)) {
157 t = &pckbc_consdata;
158 ioh_d = t->t_ioh_d;
159 ioh_c = t->t_ioh_c;
160 pckbc_console_attached = 1;
161 /* t->t_cmdbyte was initialized by cnattach */
162 } else {
163 if (pnpbios_io_map(aa->pbt, aa->resc, 0,
164 &iot, &ioh_d) ||
165 pnpbios_io_map(aa->pbt, aa->resc, 1,
166 &iot, &ioh_c))
167 panic("pckbc_pnpbios_attach: couldn't map");
168
169 t = malloc(sizeof(struct pckbc_internal),
170 M_DEVBUF, M_WAITOK);
171 memset(t, 0, sizeof(*t));
172 t->t_iot = iot;
173 t->t_ioh_d = ioh_d;
174 t->t_ioh_c = ioh_c;
175 t->t_addr = iobase;
176 t->t_cmdbyte = KC8_CPU; /* Enable ports */
177 callout_init(&t->t_cleanup);
178 }
179
180 t->t_sc = sc;
181 sc->id = t;
182 }
183
184 for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
185 peer_psc = pckbc_cd.cd_devs[i];
186 if (peer_psc != NULL &&
187 peer_psc != psc &&
188 peer_psc->sc_pckbc.sc_dv.dv_parent ==
189 psc->sc_pckbc.sc_dv.dv_parent)
190 break;
191 peer_psc = NULL;
192 }
193
194 if (peer_psc != NULL) {
195 /*
196 * Have both -- finish attaching the one marked "keyboard".
197 */
198 if (psc->sc_slot == PCKBC_KBD_SLOT) {
199 kbd_psc = psc;
200 kbd_psc->sc_aux_port = peer_psc;
201 } else {
202 kbd_psc = peer_psc;
203 kbd_psc->sc_aux_port = psc;
204 }
205 pckbc_attach(&kbd_psc->sc_pckbc);
206 }
207 }
208
209 void
210 pckbc_pnpbios_intr_establish(struct pckbc_softc *sc,
211 pckbc_slot_t slot)
212 {
213 struct pckbc_pnpbios_softc *psc = (void *) sc;
214 void *rv;
215 int irq, ist;
216
217 /*
218 * Note we're always called with the keyboard slot.
219 */
220
221 if (slot == PCKBC_KBD_SLOT) {
222 irq = psc->sc_irq;
223 ist = psc->sc_ist;
224 } else {
225 irq = psc->sc_aux_port->sc_irq;
226 ist = psc->sc_aux_port->sc_ist;
227 }
228
229 rv = isa_intr_establish(0/*XXX*/, irq, ist, IPL_TTY, pckbcintr, sc);
230 if (rv == NULL) {
231 printf("%s: unable to establish interrupt for %s slot\n",
232 sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
233 } else {
234 printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
235 irq, pckbc_slot_names[slot]);
236 }
237 }
238