ms_hb.c revision 1.1 1 1.1 tsubai /* $NetBSD: ms_hb.c,v 1.1 2000/11/13 16:48:45 tsubai Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*-
4 1.1 tsubai * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 1.1 tsubai *
6 1.1 tsubai * Redistribution and use in source and binary forms, with or without
7 1.1 tsubai * modification, are permitted provided that the following conditions
8 1.1 tsubai * are met:
9 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
10 1.1 tsubai * notice, this list of conditions and the following disclaimer.
11 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
13 1.1 tsubai * documentation and/or other materials provided with the distribution.
14 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products
15 1.1 tsubai * derived from this software without specific prior written permission.
16 1.1 tsubai *
17 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 tsubai */
28 1.1 tsubai
29 1.1 tsubai #include <sys/param.h>
30 1.1 tsubai #include <sys/device.h>
31 1.1 tsubai #include <sys/systm.h>
32 1.1 tsubai
33 1.1 tsubai #include <dev/wscons/wsconsio.h>
34 1.1 tsubai #include <dev/wscons/wsmousevar.h>
35 1.1 tsubai
36 1.1 tsubai #include <machine/autoconf.h>
37 1.1 tsubai #include <machine/adrsmap.h>
38 1.1 tsubai
39 1.1 tsubai struct msreg {
40 1.1 tsubai u_char ms_data;
41 1.1 tsubai u_char ms_stat;
42 1.1 tsubai u_char ms_reset;
43 1.1 tsubai u_char ms_init;
44 1.1 tsubai };
45 1.1 tsubai
46 1.1 tsubai struct ms_hb_softc {
47 1.1 tsubai struct device sc_dev;
48 1.1 tsubai volatile struct msreg *sc_reg;
49 1.1 tsubai struct device *sc_wsmousedev;
50 1.1 tsubai int sc_ndata;
51 1.1 tsubai u_char sc_buf[3];
52 1.1 tsubai };
53 1.1 tsubai
54 1.1 tsubai int ms_hb_match(struct device *, struct cfdata *, void *);
55 1.1 tsubai void ms_hb_attach(struct device *, struct device *, void *);
56 1.1 tsubai int ms_hb_intr(void *);
57 1.1 tsubai
58 1.1 tsubai int ms_hb_enable(void *);
59 1.1 tsubai int ms_hb_ioctl(void *, u_long, caddr_t, int, struct proc *);
60 1.1 tsubai void ms_hb_disable(void *);
61 1.1 tsubai
62 1.1 tsubai struct cfattach ms_hb_ca = {
63 1.1 tsubai sizeof(struct ms_hb_softc), ms_hb_match, ms_hb_attach
64 1.1 tsubai };
65 1.1 tsubai
66 1.1 tsubai struct wsmouse_accessops ms_hb_accessops = {
67 1.1 tsubai ms_hb_enable,
68 1.1 tsubai ms_hb_ioctl,
69 1.1 tsubai ms_hb_disable
70 1.1 tsubai };
71 1.1 tsubai
72 1.1 tsubai int
73 1.1 tsubai ms_hb_match(parent, cf, aux)
74 1.1 tsubai struct device *parent;
75 1.1 tsubai struct cfdata *cf;
76 1.1 tsubai void *aux;
77 1.1 tsubai {
78 1.1 tsubai struct confargs *ca = aux;
79 1.1 tsubai
80 1.1 tsubai if (strcmp(ca->ca_name, "ms") == 0)
81 1.1 tsubai return 1;
82 1.1 tsubai
83 1.1 tsubai return 0;
84 1.1 tsubai }
85 1.1 tsubai
86 1.1 tsubai void
87 1.1 tsubai ms_hb_attach(parent, self, aux)
88 1.1 tsubai struct device *parent, *self;
89 1.1 tsubai void *aux;
90 1.1 tsubai {
91 1.1 tsubai struct ms_hb_softc *sc = (void *)self;
92 1.1 tsubai volatile struct msreg *reg = (void *)self->dv_cfdata->cf_addr;
93 1.1 tsubai int intr = self->dv_cfdata->cf_level;
94 1.1 tsubai struct wsmousedev_attach_args aa;
95 1.1 tsubai
96 1.1 tsubai if (intr == -1)
97 1.1 tsubai intr = 2;
98 1.1 tsubai
99 1.1 tsubai sc->sc_reg = reg;
100 1.1 tsubai reg->ms_reset = 0x01;
101 1.1 tsubai reg->ms_init = 0x80; /* 1200 bps */
102 1.1 tsubai
103 1.1 tsubai printf(" level %d\n", intr);
104 1.1 tsubai
105 1.1 tsubai hb_intr_establish(intr, IPL_TTY, ms_hb_intr, sc);
106 1.1 tsubai
107 1.1 tsubai aa.accessops = &ms_hb_accessops;
108 1.1 tsubai aa.accesscookie = sc;
109 1.1 tsubai sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint);
110 1.1 tsubai }
111 1.1 tsubai
112 1.1 tsubai int
113 1.1 tsubai ms_hb_intr(v)
114 1.1 tsubai void *v;
115 1.1 tsubai {
116 1.1 tsubai struct ms_hb_softc *sc = v;
117 1.1 tsubai volatile struct msreg *reg = sc->sc_reg;
118 1.1 tsubai volatile u_char *ien = (void *)INTEN0;
119 1.1 tsubai int code, index, byte0, byte1, byte2;
120 1.1 tsubai int button, dx, dy;
121 1.1 tsubai int rv = 0;
122 1.1 tsubai
123 1.1 tsubai *ien &= ~RX_MSINTE;
124 1.1 tsubai
125 1.1 tsubai while (reg->ms_stat & RX_MSRDY) {
126 1.1 tsubai rv = 1;
127 1.1 tsubai code = reg->ms_data;
128 1.1 tsubai index = sc->sc_ndata;
129 1.1 tsubai
130 1.1 tsubai if (sc->sc_wsmousedev == NULL)
131 1.1 tsubai continue;
132 1.1 tsubai
133 1.1 tsubai if (code & 0x80) {
134 1.1 tsubai sc->sc_buf[0] = code;
135 1.1 tsubai sc->sc_ndata = 1;
136 1.1 tsubai continue;
137 1.1 tsubai }
138 1.1 tsubai
139 1.1 tsubai if (index == 1) {
140 1.1 tsubai sc->sc_buf[1] = code;
141 1.1 tsubai sc->sc_ndata++;
142 1.1 tsubai continue;
143 1.1 tsubai }
144 1.1 tsubai
145 1.1 tsubai if (index == 2) {
146 1.1 tsubai sc->sc_buf[2] = code;
147 1.1 tsubai sc->sc_ndata++;
148 1.1 tsubai
149 1.1 tsubai byte0 = sc->sc_buf[0];
150 1.1 tsubai byte1 = sc->sc_buf[1];
151 1.1 tsubai byte2 = sc->sc_buf[2];
152 1.1 tsubai
153 1.1 tsubai button = 0;
154 1.1 tsubai if (byte0 & 0x01)
155 1.1 tsubai button |= 1;
156 1.1 tsubai if (byte0 & 0x04)
157 1.1 tsubai button |= 2;
158 1.1 tsubai if (byte0 & 0x02)
159 1.1 tsubai button |= 4;
160 1.1 tsubai
161 1.1 tsubai if (byte0 & 0x08)
162 1.1 tsubai dx = byte1 - 0x80;
163 1.1 tsubai else
164 1.1 tsubai dx = byte1;
165 1.1 tsubai if (byte0 & 0x10)
166 1.1 tsubai dy = byte2 - 0x80;
167 1.1 tsubai else
168 1.1 tsubai dy = byte2;
169 1.1 tsubai
170 1.1 tsubai wsmouse_input(sc->sc_wsmousedev, button, dx, -dy, 0,
171 1.1 tsubai WSMOUSE_INPUT_DELTA);
172 1.1 tsubai }
173 1.1 tsubai }
174 1.1 tsubai
175 1.1 tsubai *ien |= RX_MSINTE;
176 1.1 tsubai return rv;
177 1.1 tsubai }
178 1.1 tsubai
179 1.1 tsubai int
180 1.1 tsubai ms_hb_enable(v)
181 1.1 tsubai void *v;
182 1.1 tsubai {
183 1.1 tsubai volatile u_char *ien = (void *)INTEN0;
184 1.1 tsubai
185 1.1 tsubai *ien |= RX_MSINTE;
186 1.1 tsubai return 0;
187 1.1 tsubai }
188 1.1 tsubai
189 1.1 tsubai void
190 1.1 tsubai ms_hb_disable(v)
191 1.1 tsubai void *v;
192 1.1 tsubai {
193 1.1 tsubai volatile u_char *ien = (void *)INTEN0;
194 1.1 tsubai
195 1.1 tsubai *ien &= ~RX_MSINTE;
196 1.1 tsubai }
197 1.1 tsubai
198 1.1 tsubai int
199 1.1 tsubai ms_hb_ioctl(v, cmd, data, flag, p)
200 1.1 tsubai void *v;
201 1.1 tsubai u_long cmd;
202 1.1 tsubai caddr_t data;
203 1.1 tsubai int flag;
204 1.1 tsubai struct proc *p;
205 1.1 tsubai {
206 1.1 tsubai return -1;
207 1.1 tsubai }
208