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>
46 /* mouse device commands */
47 #define PS2MC_RESET_DEV 0xff
48 #define PS2MC_SET_DEFAULTS 0xf6
49 #define PS2MC_DISABLE 0xf5
50 #define PS2MC_ENABLE 0xf4
51 #define PS2MC_SET_SAMPLING_RATE 0xf3
52 #define PS2MC_SEND_DEV_ID 0xf2
53 #define PS2MC_SET_REMOTE_MODE 0xf0
54 #define PS2MC_SEND_DEV_DATA 0xeb
55 #define PS2MC_SET_STREAM_MODE 0xea
56 #define PS2MC_SEND_DEV_STATUS 0xe9
57 #define PS2MC_SET_RESOLUTION 0xe8
58 #define PS2MC_SET_SCALING1 0xe7
59 #define PS2MC_SET_SCALING2 0xe6
61 #define PS2MC_BAT_SUCCESS 0xaa
62 #define PS2MC_ACK 0xfa
65 #define PS2MOUSE_DEV_ID 0x0
68 #define PS2M_DATA_Y_OFLOW 0x80
69 #define PS2M_DATA_X_OFLOW 0x40
70 #define PS2M_DATA_Y_SIGN 0x20
71 #define PS2M_DATA_X_SIGN 0x10
72 #define PS2M_DATA_AONE 0x08
73 #define PS2M_DATA_MID_BUTTON 0x04
74 #define PS2M_DATA_RIGHT_BUTTON 0x02
75 #define PS2M_DATA_LEFT_BUTTON 0x01
77 /* mouse status bits */
78 #define PS2M_STS_REMOTE_MODE 0x40
79 #define PS2M_STS_ENABLE_DEV 0x20
80 #define PS2M_STS_SCALING_21 0x10
81 #define PS2M_STS_MID_BUTTON 0x04
82 #define PS2M_STS_RIGHT_BUTTON 0x02
83 #define PS2M_STS_LEFT_BUTTON 0x01
85 #define PS2MOUSE_FIFOSZ 16
88 uint8_t buf[PS2MOUSE_FIFOSZ];
89 int rindex; /* index to read from */
90 int windex; /* index to write to */
91 int num; /* number of bytes in the fifo */
92 int size; /* size of the fifo */
95 struct ps2mouse_softc {
96 struct atkbdc_softc *atkbdc_sc;
101 uint8_t sampling_rate;
105 uint8_t curcmd; /* current command for next byte */
108 int delta_x, delta_y;
112 fifo_init(struct ps2mouse_softc *sc)
117 fifo->size = sizeof(((struct fifo *)0)->buf);
121 fifo_reset(struct ps2mouse_softc *sc)
126 bzero(fifo, sizeof(struct fifo));
127 fifo->size = sizeof(((struct fifo *)0)->buf);
131 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
136 if (fifo->num < fifo->size) {
137 fifo->buf[fifo->windex] = val;
138 fifo->windex = (fifo->windex + 1) % fifo->size;
144 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
150 *val = fifo->buf[fifo->rindex];
151 fifo->rindex = (fifo->rindex + 1) % fifo->size;
160 movement_reset(struct ps2mouse_softc *sc)
162 assert(pthread_mutex_isowned_np(&sc->mtx));
169 movement_update(struct ps2mouse_softc *sc, int x, int y)
171 sc->delta_x += x - sc->cur_x;
172 sc->delta_y += sc->cur_y - y;
178 movement_get(struct ps2mouse_softc *sc)
180 uint8_t val0, val1, val2;
182 assert(pthread_mutex_isowned_np(&sc->mtx));
184 val0 = PS2M_DATA_AONE;
185 val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
186 PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
188 if (sc->delta_x >= 0) {
189 if (sc->delta_x > 255) {
190 val0 |= PS2M_DATA_X_OFLOW;
195 val0 |= PS2M_DATA_X_SIGN;
196 if (sc->delta_x < -255) {
197 val0 |= PS2M_DATA_X_OFLOW;
204 if (sc->delta_y >= 0) {
205 if (sc->delta_y > 255) {
206 val0 |= PS2M_DATA_Y_OFLOW;
211 val0 |= PS2M_DATA_Y_SIGN;
212 if (sc->delta_y < -255) {
213 val0 |= PS2M_DATA_Y_OFLOW;
220 if (sc->fifo.num < (sc->fifo.size - 3)) {
228 ps2mouse_reset(struct ps2mouse_softc *sc)
230 assert(pthread_mutex_isowned_np(&sc->mtx));
233 sc->status = PS2M_STS_ENABLE_DEV;
235 sc->sampling_rate = 100;
244 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
248 pthread_mutex_lock(&sc->mtx);
249 retval = fifo_get(sc, val);
250 pthread_mutex_unlock(&sc->mtx);
256 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
258 return (sc->fifo.num);
262 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
264 pthread_mutex_lock(&sc->mtx);
273 pthread_mutex_unlock(&sc->mtx);
277 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
279 pthread_mutex_lock(&sc->mtx);
282 switch (sc->curcmd) {
283 case PS2MC_SET_SAMPLING_RATE:
284 sc->sampling_rate = val;
285 fifo_put(sc, PS2MC_ACK);
287 case PS2MC_SET_RESOLUTION:
288 sc->resolution = val;
289 fifo_put(sc, PS2MC_ACK);
292 fprintf(stderr, "Unhandled ps2 mouse current "
293 "command byte 0x%02x\n", val);
303 fifo_put(sc, PS2MC_ACK);
305 case PS2MC_RESET_DEV:
307 fifo_put(sc, PS2MC_ACK);
308 fifo_put(sc, PS2MC_BAT_SUCCESS);
309 fifo_put(sc, PS2MOUSE_DEV_ID);
311 case PS2MC_SET_DEFAULTS:
313 fifo_put(sc, PS2MC_ACK);
317 sc->status &= ~PS2M_STS_ENABLE_DEV;
318 fifo_put(sc, PS2MC_ACK);
322 sc->status |= PS2M_STS_ENABLE_DEV;
323 fifo_put(sc, PS2MC_ACK);
325 case PS2MC_SET_SAMPLING_RATE:
327 fifo_put(sc, PS2MC_ACK);
329 case PS2MC_SEND_DEV_ID:
330 fifo_put(sc, PS2MC_ACK);
331 fifo_put(sc, PS2MOUSE_DEV_ID);
333 case PS2MC_SET_REMOTE_MODE:
334 sc->status |= PS2M_STS_REMOTE_MODE;
335 fifo_put(sc, PS2MC_ACK);
337 case PS2MC_SEND_DEV_DATA:
338 fifo_put(sc, PS2MC_ACK);
341 case PS2MC_SET_STREAM_MODE:
342 sc->status &= ~PS2M_STS_REMOTE_MODE;
343 fifo_put(sc, PS2MC_ACK);
345 case PS2MC_SEND_DEV_STATUS:
346 fifo_put(sc, PS2MC_ACK);
347 fifo_put(sc, sc->status);
348 fifo_put(sc, sc->resolution);
349 fifo_put(sc, sc->sampling_rate);
351 case PS2MC_SET_RESOLUTION:
353 fifo_put(sc, PS2MC_ACK);
355 case PS2MC_SET_SCALING1:
356 case PS2MC_SET_SCALING2:
357 fifo_put(sc, PS2MC_ACK);
360 fifo_put(sc, PS2MC_ACK);
361 fprintf(stderr, "Unhandled ps2 mouse command "
366 pthread_mutex_unlock(&sc->mtx);
370 ps2mouse_event(uint8_t button, int x, int y, void *arg)
372 struct ps2mouse_softc *sc = arg;
374 pthread_mutex_lock(&sc->mtx);
375 movement_update(sc, x, y);
377 sc->status &= ~(PS2M_STS_LEFT_BUTTON |
378 PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
379 if (button & (1 << 0))
380 sc->status |= PS2M_STS_LEFT_BUTTON;
381 if (button & (1 << 1))
382 sc->status |= PS2M_STS_MID_BUTTON;
383 if (button & (1 << 2))
384 sc->status |= PS2M_STS_RIGHT_BUTTON;
386 if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
387 /* no data reporting */
388 pthread_mutex_unlock(&sc->mtx);
393 pthread_mutex_unlock(&sc->mtx);
395 if (sc->fifo.num > 0)
396 atkbdc_event(sc->atkbdc_sc, 0);
399 struct ps2mouse_softc *
400 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
402 struct ps2mouse_softc *sc;
404 sc = calloc(1, sizeof (struct ps2mouse_softc));
405 pthread_mutex_init(&sc->mtx, NULL);
407 sc->atkbdc_sc = atkbdc_sc;
409 pthread_mutex_lock(&sc->mtx);
411 pthread_mutex_unlock(&sc->mtx);
413 console_ptr_register(ps2mouse_event, sc, 1);