2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5 * Copyright (c) 2015 Nahanni Systems Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
41 #include <pthread_np.h>
47 /* mouse device commands */
48 #define PS2MC_RESET_DEV 0xff
49 #define PS2MC_SET_DEFAULTS 0xf6
50 #define PS2MC_DISABLE 0xf5
51 #define PS2MC_ENABLE 0xf4
52 #define PS2MC_SET_SAMPLING_RATE 0xf3
53 #define PS2MC_SEND_DEV_ID 0xf2
54 #define PS2MC_SET_REMOTE_MODE 0xf0
55 #define PS2MC_SEND_DEV_DATA 0xeb
56 #define PS2MC_SET_STREAM_MODE 0xea
57 #define PS2MC_SEND_DEV_STATUS 0xe9
58 #define PS2MC_SET_RESOLUTION 0xe8
59 #define PS2MC_SET_SCALING1 0xe7
60 #define PS2MC_SET_SCALING2 0xe6
62 #define PS2MC_BAT_SUCCESS 0xaa
63 #define PS2MC_ACK 0xfa
66 #define PS2MOUSE_DEV_ID 0x0
69 #define PS2M_DATA_Y_OFLOW 0x80
70 #define PS2M_DATA_X_OFLOW 0x40
71 #define PS2M_DATA_Y_SIGN 0x20
72 #define PS2M_DATA_X_SIGN 0x10
73 #define PS2M_DATA_AONE 0x08
74 #define PS2M_DATA_MID_BUTTON 0x04
75 #define PS2M_DATA_RIGHT_BUTTON 0x02
76 #define PS2M_DATA_LEFT_BUTTON 0x01
78 /* mouse status bits */
79 #define PS2M_STS_REMOTE_MODE 0x40
80 #define PS2M_STS_ENABLE_DEV 0x20
81 #define PS2M_STS_SCALING_21 0x10
82 #define PS2M_STS_MID_BUTTON 0x04
83 #define PS2M_STS_RIGHT_BUTTON 0x02
84 #define PS2M_STS_LEFT_BUTTON 0x01
86 #define PS2MOUSE_FIFOSZ 16
89 uint8_t buf[PS2MOUSE_FIFOSZ];
90 int rindex; /* index to read from */
91 int windex; /* index to write to */
92 int num; /* number of bytes in the fifo */
93 int size; /* size of the fifo */
96 struct ps2mouse_softc {
97 struct atkbdc_softc *atkbdc_sc;
102 uint8_t sampling_rate;
106 uint8_t curcmd; /* current command for next byte */
109 int delta_x, delta_y;
113 fifo_init(struct ps2mouse_softc *sc)
118 fifo->size = sizeof(((struct fifo *)0)->buf);
122 fifo_reset(struct ps2mouse_softc *sc)
127 bzero(fifo, sizeof(struct fifo));
128 fifo->size = sizeof(((struct fifo *)0)->buf);
132 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
137 if (fifo->num < fifo->size) {
138 fifo->buf[fifo->windex] = val;
139 fifo->windex = (fifo->windex + 1) % fifo->size;
145 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
151 *val = fifo->buf[fifo->rindex];
152 fifo->rindex = (fifo->rindex + 1) % fifo->size;
161 movement_reset(struct ps2mouse_softc *sc)
163 assert(pthread_mutex_isowned_np(&sc->mtx));
170 movement_update(struct ps2mouse_softc *sc, int x, int y)
172 sc->delta_x += x - sc->cur_x;
173 sc->delta_y += sc->cur_y - y;
179 movement_get(struct ps2mouse_softc *sc)
181 uint8_t val0, val1, val2;
183 assert(pthread_mutex_isowned_np(&sc->mtx));
185 val0 = PS2M_DATA_AONE;
186 val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
187 PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
189 if (sc->delta_x >= 0) {
190 if (sc->delta_x > 255) {
191 val0 |= PS2M_DATA_X_OFLOW;
196 val0 |= PS2M_DATA_X_SIGN;
197 if (sc->delta_x < -255) {
198 val0 |= PS2M_DATA_X_OFLOW;
205 if (sc->delta_y >= 0) {
206 if (sc->delta_y > 255) {
207 val0 |= PS2M_DATA_Y_OFLOW;
212 val0 |= PS2M_DATA_Y_SIGN;
213 if (sc->delta_y < -255) {
214 val0 |= PS2M_DATA_Y_OFLOW;
221 if (sc->fifo.num < (sc->fifo.size - 3)) {
229 ps2mouse_reset(struct ps2mouse_softc *sc)
231 assert(pthread_mutex_isowned_np(&sc->mtx));
234 sc->status = PS2M_STS_ENABLE_DEV;
236 sc->sampling_rate = 100;
245 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
249 pthread_mutex_lock(&sc->mtx);
250 retval = fifo_get(sc, val);
251 pthread_mutex_unlock(&sc->mtx);
257 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
259 return (sc->fifo.num);
263 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
265 pthread_mutex_lock(&sc->mtx);
274 pthread_mutex_unlock(&sc->mtx);
278 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
280 pthread_mutex_lock(&sc->mtx);
283 switch (sc->curcmd) {
284 case PS2MC_SET_SAMPLING_RATE:
285 sc->sampling_rate = val;
286 fifo_put(sc, PS2MC_ACK);
288 case PS2MC_SET_RESOLUTION:
289 sc->resolution = val;
290 fifo_put(sc, PS2MC_ACK);
293 EPRINTLN("Unhandled ps2 mouse current "
294 "command byte 0x%02x", val);
304 fifo_put(sc, PS2MC_ACK);
306 case PS2MC_RESET_DEV:
308 fifo_put(sc, PS2MC_ACK);
309 fifo_put(sc, PS2MC_BAT_SUCCESS);
310 fifo_put(sc, PS2MOUSE_DEV_ID);
312 case PS2MC_SET_DEFAULTS:
314 fifo_put(sc, PS2MC_ACK);
318 sc->status &= ~PS2M_STS_ENABLE_DEV;
319 fifo_put(sc, PS2MC_ACK);
323 sc->status |= PS2M_STS_ENABLE_DEV;
324 fifo_put(sc, PS2MC_ACK);
326 case PS2MC_SET_SAMPLING_RATE:
328 fifo_put(sc, PS2MC_ACK);
330 case PS2MC_SEND_DEV_ID:
331 fifo_put(sc, PS2MC_ACK);
332 fifo_put(sc, PS2MOUSE_DEV_ID);
334 case PS2MC_SET_REMOTE_MODE:
335 sc->status |= PS2M_STS_REMOTE_MODE;
336 fifo_put(sc, PS2MC_ACK);
338 case PS2MC_SEND_DEV_DATA:
339 fifo_put(sc, PS2MC_ACK);
342 case PS2MC_SET_STREAM_MODE:
343 sc->status &= ~PS2M_STS_REMOTE_MODE;
344 fifo_put(sc, PS2MC_ACK);
346 case PS2MC_SEND_DEV_STATUS:
347 fifo_put(sc, PS2MC_ACK);
348 fifo_put(sc, sc->status);
349 fifo_put(sc, sc->resolution);
350 fifo_put(sc, sc->sampling_rate);
352 case PS2MC_SET_RESOLUTION:
354 fifo_put(sc, PS2MC_ACK);
356 case PS2MC_SET_SCALING1:
357 case PS2MC_SET_SCALING2:
358 fifo_put(sc, PS2MC_ACK);
361 fifo_put(sc, PS2MC_ACK);
362 EPRINTLN("Unhandled ps2 mouse command "
367 pthread_mutex_unlock(&sc->mtx);
371 ps2mouse_event(uint8_t button, int x, int y, void *arg)
373 struct ps2mouse_softc *sc = arg;
375 pthread_mutex_lock(&sc->mtx);
376 movement_update(sc, x, y);
378 sc->status &= ~(PS2M_STS_LEFT_BUTTON |
379 PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
380 if (button & (1 << 0))
381 sc->status |= PS2M_STS_LEFT_BUTTON;
382 if (button & (1 << 1))
383 sc->status |= PS2M_STS_MID_BUTTON;
384 if (button & (1 << 2))
385 sc->status |= PS2M_STS_RIGHT_BUTTON;
387 if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
388 /* no data reporting */
389 pthread_mutex_unlock(&sc->mtx);
394 pthread_mutex_unlock(&sc->mtx);
396 if (sc->fifo.num > 0)
397 atkbdc_event(sc->atkbdc_sc, 0);
400 struct ps2mouse_softc *
401 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
403 struct ps2mouse_softc *sc;
405 sc = calloc(1, sizeof (struct ps2mouse_softc));
406 pthread_mutex_init(&sc->mtx, NULL);
408 sc->atkbdc_sc = atkbdc_sc;
410 pthread_mutex_lock(&sc->mtx);
412 pthread_mutex_unlock(&sc->mtx);
414 console_ptr_register(ps2mouse_event, sc, 1);