xref: /freebsd/sys/dev/atkbdc/atkbd_atkbdc.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "atkbd.h"
30 #include "opt_kbd.h"
31 
32 #if NATKBD > 0
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/tty.h>
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 
43 #include <machine/resource.h>
44 
45 #include <dev/kbd/kbdreg.h>
46 #include <dev/kbd/atkbdreg.h>
47 #include <dev/kbd/atkbdcreg.h>
48 
49 #include <isa/isareg.h>
50 #include <isa/isavar.h>
51 
52 devclass_t	atkbd_devclass;
53 
54 static int	atkbdprobe(device_t dev);
55 static int	atkbdattach(device_t dev);
56 static void	atkbd_isa_intr(void *arg);
57 
58 static device_method_t atkbd_methods[] = {
59 	DEVMETHOD(device_probe,		atkbdprobe),
60 	DEVMETHOD(device_attach,	atkbdattach),
61 	{ 0, 0 }
62 };
63 
64 static driver_t atkbd_driver = {
65 	ATKBD_DRIVER_NAME,
66 	atkbd_methods,
67 	1,
68 };
69 
70 static int
71 atkbdprobe(device_t dev)
72 {
73 	uintptr_t port;
74 	uintptr_t irq;
75 	uintptr_t flags;
76 
77 	device_set_desc(dev, "AT Keyboard");
78 
79 	/* obtain parameters */
80 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_PORT, &port);
81 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
82 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
83 
84 	/* probe the device */
85 	return atkbd_probe_unit(device_get_unit(dev), port, irq, flags);
86 }
87 
88 static int
89 atkbdattach(device_t dev)
90 {
91 	keyboard_t *kbd;
92 	uintptr_t port;
93 	uintptr_t irq;
94 	uintptr_t flags;
95 	struct resource *res;
96 	void *ih;
97 	int zero = 0;
98 	int error;
99 
100 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_PORT, &port);
101 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
102 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
103 
104 	error = atkbd_attach_unit(device_get_unit(dev), &kbd, port, irq, flags);
105 	if (error)
106 		return error;
107 
108 	/* declare our interrupt handler */
109 	res = bus_alloc_resource(dev, SYS_RES_IRQ, &zero, irq, irq, 1,
110 				 RF_SHAREABLE | RF_ACTIVE);
111 	BUS_SETUP_INTR(device_get_parent(dev), dev, res, INTR_TYPE_TTY,
112 		       atkbd_isa_intr, kbd, &ih);
113 
114 	return 0;
115 }
116 
117 static void
118 atkbd_isa_intr(void *arg)
119 {
120 	keyboard_t *kbd;
121 
122 	kbd = (keyboard_t *)arg;
123 	(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
124 }
125 
126 DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, atkbd_devclass, 0, 0);
127 
128 #endif /* NATKBD > 0 */
129