xref: /freebsd/sys/dev/atkbdc/atkbd_atkbdc.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 "opt_kbd.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/conf.h>
35 #include <sys/tty.h>
36 #include <sys/bus.h>
37 #include <machine/bus.h>
38 #include <sys/rman.h>
39 
40 #include <machine/resource.h>
41 
42 #include <dev/kbd/kbdreg.h>
43 #include <dev/kbd/atkbdreg.h>
44 #include <dev/kbd/atkbdcreg.h>
45 
46 #include <isa/isareg.h>
47 #include <isa/isavar.h>
48 
49 devclass_t	atkbd_devclass;
50 
51 static int	atkbdprobe(device_t dev);
52 static int	atkbdattach(device_t dev);
53 static void	atkbd_isa_intr(void *arg);
54 
55 static device_method_t atkbd_methods[] = {
56 	DEVMETHOD(device_probe,		atkbdprobe),
57 	DEVMETHOD(device_attach,	atkbdattach),
58 	{ 0, 0 }
59 };
60 
61 static driver_t atkbd_driver = {
62 	ATKBD_DRIVER_NAME,
63 	atkbd_methods,
64 	1,
65 };
66 
67 static int
68 atkbdprobe(device_t dev)
69 {
70 	uintptr_t port;
71 	uintptr_t irq;
72 	uintptr_t flags;
73 
74 	device_set_desc(dev, "AT Keyboard");
75 
76 	/* obtain parameters */
77 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_PORT, &port);
78 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
79 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
80 
81 	/* probe the device */
82 	return atkbd_probe_unit(device_get_unit(dev), port, irq, flags);
83 }
84 
85 static int
86 atkbdattach(device_t dev)
87 {
88 	keyboard_t *kbd;
89 	uintptr_t port;
90 	uintptr_t irq;
91 	uintptr_t flags;
92 	struct resource *res;
93 	void *ih;
94 	int zero = 0;
95 	int error;
96 
97 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_PORT, &port);
98 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
99 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
100 
101 	error = atkbd_attach_unit(device_get_unit(dev), &kbd, port, irq, flags);
102 	if (error)
103 		return error;
104 
105 	/* declare our interrupt handler */
106 	res = bus_alloc_resource(dev, SYS_RES_IRQ, &zero, irq, irq, 1,
107 				 RF_SHAREABLE | RF_ACTIVE);
108 	BUS_SETUP_INTR(device_get_parent(dev), dev, res, INTR_TYPE_TTY,
109 		       atkbd_isa_intr, kbd, &ih);
110 
111 	return 0;
112 }
113 
114 static void
115 atkbd_isa_intr(void *arg)
116 {
117 	keyboard_t *kbd;
118 
119 	kbd = (keyboard_t *)arg;
120 	(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
121 }
122 
123 DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, atkbd_devclass, 0, 0);
124