56273bfda991691debb8a2e58b4f64d0d510e535
[dragonfly.git] / sys / dev / misc / atkbd / atkbd_isa.c
1 /*-
2  * (MPSAFE)
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/isa/atkbd_isa.c,v 1.7.2.3 2001/08/01 10:42:28 yokota Exp $
29  */
30
31 #include "opt_kbd.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/thread.h>
39 #include <sys/machintr.h>
40
41 #include <sys/kbio.h>
42 #include <dev/misc/kbd/kbdreg.h>
43 #include <dev/misc/kbd/atkbdreg.h>
44 #include <dev/misc/kbd/atkbdcreg.h>
45
46 #include <bus/isa/isareg.h>
47 #include <bus/isa/isavar.h>
48
49 #if 0
50 #define lwkt_gettoken(x)
51 #define lwkt_reltoken(x)
52 #endif
53
54 typedef struct {
55         struct resource *intr;
56         void            *ih;
57 } atkbd_softc_t;
58
59 devclass_t      atkbd_devclass;
60
61 static int      atkbdprobe(device_t dev);
62 static int      atkbdattach(device_t dev);
63 static int      atkbdresume(device_t dev);
64 static void     atkbd_isa_intr(void *arg);
65
66 static device_method_t atkbd_methods[] = {
67         DEVMETHOD(device_probe,         atkbdprobe),
68         DEVMETHOD(device_attach,        atkbdattach),
69         DEVMETHOD(device_resume,        atkbdresume),
70         DEVMETHOD_END
71 };
72
73 static driver_t atkbd_driver = {
74         ATKBD_DRIVER_NAME,
75         atkbd_methods,
76         sizeof(atkbd_softc_t),
77 };
78
79 static int
80 atkbdprobe(device_t dev)
81 {
82         uintptr_t irq;
83         uintptr_t flags;
84
85         device_set_desc(dev, "AT Keyboard");
86
87         /* obtain parameters */
88         BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
89         BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
90
91         /* probe the device */
92         return atkbd_probe_unit(device_get_unit(dev),
93                                 device_get_unit(device_get_parent(dev)),
94                                 irq, flags);
95 }
96
97 static int
98 atkbdattach(device_t dev)
99 {
100         atkbd_softc_t *sc;
101         keyboard_t *kbd;
102         uintptr_t irq;
103         uintptr_t flags;
104         int rid;
105         int error;
106
107         sc = device_get_softc(dev);
108
109         BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
110         BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
111
112         error = atkbd_attach_unit(device_get_unit(dev), &kbd,
113                                   device_get_unit(device_get_parent(dev)),
114                                   irq, flags);
115         if (error)
116                 return error;
117
118         /* declare our interrupt handler */
119         rid = 0;
120         sc->intr = bus_alloc_legacy_irq_resource(dev, &rid, irq, RF_ACTIVE);
121         BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr, INTR_MPSAFE,
122                        atkbd_isa_intr, kbd, &sc->ih, NULL, NULL);
123
124         return 0;
125 }
126
127 static int
128 atkbdresume(device_t dev)
129 {
130         atkbd_softc_t *sc;
131         keyboard_t *kbd;
132         int args[2];
133
134         lwkt_gettoken(&tty_token);
135         sc = device_get_softc(dev);
136         kbd = kbd_get_keyboard(kbd_find_keyboard(ATKBD_DRIVER_NAME,
137                                                  device_get_unit(dev)));
138         if (kbd) {
139                 kbd->kb_flags &= ~KB_INITIALIZED;
140                 args[0] = device_get_unit(device_get_parent(dev));
141                 args[1] = rman_get_start(sc->intr);
142                 sw_init(kbdsw[kbd->kb_index], device_get_unit(dev), &kbd,
143                                               args, device_get_flags(dev));
144                 kbd_clear_state(kbd);
145
146         }
147         lwkt_reltoken(&tty_token);
148         return 0;
149 }
150
151 static void
152 atkbd_isa_intr(void *arg)
153 {
154         keyboard_t *kbd;
155
156         lwkt_gettoken(&tty_token);
157         kbd = (keyboard_t *)arg;
158         kbd_intr(kbd, NULL);
159         lwkt_reltoken(&tty_token);
160 }
161
162 DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, atkbd_devclass, NULL, NULL);