1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/* $NetBSD: optpoint.c,v 1.9 2021/08/07 16:18:54 thorpej Exp $ */
/*-
* Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* OptOpint on Telios HC-AJ2
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: optpoint.c,v 1.9 2021/08/07 16:18:54 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/config_hook.h>
#include <dev/hpc/hpciovar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <hpcmips/tx/tx39var.h>
#include <hpcmips/tx/tx39spivar.h>
#include <hpcmips/tx/tx39icureg.h>
#undef OPTPOINTDEBUG
#ifdef OPTPOINTDEBUG
#define DPRINTF(arg) printf arg
#else
#define DPRINTF(arg)
#endif
struct optpoint_softc {
device_t sc_dev;
tx_chipset_tag_t sc_tc;
struct tx39spi_softc *sc_spi;
struct hpcio_chip *sc_hc;
device_t sc_wsmousedev;
void *sc_powerhook; /* power management hook */
char packet[4];
int index; /* number of bytes received for this packet */
u_int buttons; /* mouse button status */
int enabled;
};
static int optpoint_match(device_t, cfdata_t, void *);
static void optpoint_attach(device_t, device_t, void *);
static int optpoint_intr(void *);
static int optpoint_enable(void *);
static void optpoint_disable(void *);
static int optpoint_ioctl(void *, u_long, void *, int, struct lwp *);
static int optpoint_initialize(void *);
static void optpoint_send(struct optpoint_softc *, int);
static int optpoint_recv(struct optpoint_softc *);
static int optpoint_power(void *, int, long, void *);
#define LBUTMASK 0x01
#define RBUTMASK 0x02
#define TX39_INTRSTATUS4_OPTPOINTINT TX39_INTRSTATUS4_CARDIORDNEGINT
#define TX39_IO_MFIO_CARDREG 11
#define TX39_IO_MFIO_CARDIOWR 10
#define TX39_IO_MFIO_CARDIORD 9
#define TELIOS_MFIO_OPTP_T_RDY TX39_IO_MFIO_CARDREG
#define TELIOS_MFIO_OPTP_C_REQ TX39_IO_MFIO_CARDIOWR
#define TELIOS_MFIO_OPTP_S_ENB_N TX39_IO_MFIO_CARDIORD
CFATTACH_DECL_NEW(optpoint, sizeof(struct optpoint_softc),
optpoint_match, optpoint_attach, NULL, NULL);
const struct wsmouse_accessops optpoint_accessops = {
optpoint_enable,
optpoint_ioctl,
optpoint_disable,
};
int
optpoint_match(device_t parent, cfdata_t cf, void *aux)
{
return (ATTACH_NORMAL);
}
void
optpoint_attach(device_t parent, device_t self, void *aux)
{
struct txspi_attach_args *ta = aux;
struct optpoint_softc *sc = device_private(self);
struct tx39spi_softc *spi = sc->sc_spi = device_private(parent);
tx_chipset_tag_t tc = sc->sc_tc = ta->sa_tc;
struct wsmousedev_attach_args wsmaa;
sc->sc_dev = self;
sc->sc_hc = tc->tc_iochip[MFIO];
sc->enabled = 0;
/* Specific SPI settings for OptPoint of HC-AJ2 */
tx39spi_delayval(spi, 0);
tx39spi_baudrate(spi, 4); /* SPICLK Rate = 737.3 kHz */
tx39spi_word(spi, 0); /* Use 8bits of data */
tx39spi_phapol(spi, 0);
tx39spi_clkpol(spi, 1);
tx39spi_lsb(spi, 0); /* MSB first */
optpoint_enable(sc);
printf("\n");
wsmaa.accessops = &optpoint_accessops;
wsmaa.accesscookie = sc;
/* attach the wsmouse */
sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint,
CFARGS_NONE);
/* Add a hard power hook to power saving */
sc->sc_powerhook = config_hook(CONFIG_HOOK_PMEVENT,
CONFIG_HOOK_PMEVENT_HARDPOWER,
CONFIG_HOOK_SHARE,
optpoint_power, sc);
#ifdef DIAGNOSTIC
if (sc->sc_powerhook == 0)
printf("%s: unable to establish hard power hook",
device_xname(sc->sc_dev));
#endif
}
int
optpoint_intr(void *arg)
{
struct optpoint_softc *sc = arg;
tx_chipset_tag_t tc = sc->sc_tc;
char data = optpoint_recv(sc) & 0xff;
#ifdef DIAGNOSTIC
if (sc->index >= 3){
printf("%s: Receive buffer overflow\n", device_xname(sc->sc_dev));
sc->index = 0;
memset(sc->packet, 0, 3);
}
#endif
if ((sc->index == 1) && (data & 0xcc) != 0x08){
DPRINTF(("%s: Bad second byte (0x%02x)\n",
device_xname(sc->sc_dev), data));
tx_conf_write(tc, TX39_INTRCLEAR4_REG,
TX39_INTRSTATUS4_OPTPOINTINT);
return 0;
}
sc->packet[sc->index++] = data;
if (sc->index >= 3){
u_int newbuttons = ((sc->packet[1] & LBUTMASK) ? 0x1 : 0)
| ((sc->packet[1] & RBUTMASK) ? 0x2 : 0);
int dx = sc->packet[2];
int dy = sc->packet[0];
u_int changed = (sc->buttons ^ newbuttons);
if (dx || dy || changed){
DPRINTF(("%s: buttons=0x%x, dx=%d, dy=%d\n",
device_xname(sc->sc_dev), newbuttons, dx, dy));
wsmouse_input(sc->sc_wsmousedev,
newbuttons,
dx, dy, 0, 0,
WSMOUSE_INPUT_DELTA);
}
sc->buttons = newbuttons;
sc->index = 0;
memset(sc->packet, 0, 3);
}
tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
return 0;
}
int
optpoint_enable(void *arg)
{
struct optpoint_softc *sc = arg;
if (!sc->enabled){
tx_chipset_tag_t tc = sc->sc_tc;
struct hpcio_chip *hc = sc->sc_hc;
int s = spltty();
DPRINTF(("%s: enable\n", device_xname(sc->sc_dev)));
sc->enabled = 1;
sc->index = 0;
sc->buttons = 0;
sc->packet[0] = 0xf5; /* Disable */
sc->packet[1] = 0xf2; /* Set Stream Mode */
sc->packet[2] = 0xf8; /* Stanby */
sc->packet[3] = 0xf4; /* Enable */
tx39spi_enable(sc->sc_spi, 1);
tx_intr_establish(tc, MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
IST_EDGE, IPL_TTY, optpoint_initialize, sc);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
splx(s);
}
return 0;
}
void
optpoint_disable(void *arg)
{
struct optpoint_softc *sc = arg;
if (sc->enabled){
tx_chipset_tag_t tc = sc->sc_tc;
struct hpcio_chip *hc = sc->sc_hc;
int s = spltty();
DPRINTF(("%s: disable\n", device_xname(sc->sc_dev)));
sc->enabled = 0;
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 0);
tx_intr_disestablish(tc,
(void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
tx39spi_enable(sc->sc_spi, 0);
splx(s);
}
}
int
optpoint_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
{
switch (cmd) {
case WSMOUSEIO_GTYPE:
*(u_int *)data = WSMOUSE_TYPE_PS2;
break;
default:
return (EPASSTHROUGH);
}
return 0;
}
int
optpoint_initialize(void *arg)
{
struct optpoint_softc *sc = arg;
struct hpcio_chip *hc = sc->sc_hc;
tx_chipset_tag_t tc = sc->sc_tc;
if (sc->index < 4){
optpoint_send(sc, sc->packet[sc->index++]);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
} else {
tx_intr_disestablish(tc,
(void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
sc->index = 0;
memset(sc->packet, 0, 3);
tx_intr_establish(tc,
MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
IST_EDGE, IPL_TTY, optpoint_intr, sc);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
}
tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
return 0;
}
void
optpoint_send(struct optpoint_softc *sc, int data)
{
struct hpcio_chip *hc = sc->sc_hc;
while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
;
tx39spi_put_word(sc->sc_spi, data & 0xff);
}
int
optpoint_recv(struct optpoint_softc *sc)
{
struct hpcio_chip *hc = sc->sc_hc;
optpoint_send(sc, 0x00);
while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
;
return tx39spi_get_word(sc->sc_spi) & 0xff;
}
int
optpoint_power(void *arg, int type, long id, void *msg)
{
struct optpoint_softc *sc = arg;
int why = (int)msg;
switch (why) {
case PWR_RESUME:
/* power on */
optpoint_enable(sc);
break;
case PWR_SUSPEND:
/* FALLTHROUGH */
case PWR_STANDBY:
/* power off */
optpoint_disable(sc);
break;
}
return 0;
}
|