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>
35 #include <machine/vmm_snapshot.h>
44 #include <pthread_np.h>
50 /* mouse device commands */
51 #define PS2MC_RESET_DEV 0xff
52 #define PS2MC_SET_DEFAULTS 0xf6
53 #define PS2MC_DISABLE 0xf5
54 #define PS2MC_ENABLE 0xf4
55 #define PS2MC_SET_SAMPLING_RATE 0xf3
56 #define PS2MC_SEND_DEV_ID 0xf2
57 #define PS2MC_SET_REMOTE_MODE 0xf0
58 #define PS2MC_SEND_DEV_DATA 0xeb
59 #define PS2MC_SET_STREAM_MODE 0xea
60 #define PS2MC_SEND_DEV_STATUS 0xe9
61 #define PS2MC_SET_RESOLUTION 0xe8
62 #define PS2MC_SET_SCALING1 0xe7
63 #define PS2MC_SET_SCALING2 0xe6
65 #define PS2MC_BAT_SUCCESS 0xaa
66 #define PS2MC_ACK 0xfa
69 #define PS2MOUSE_DEV_ID 0x0
72 #define PS2M_DATA_Y_OFLOW 0x80
73 #define PS2M_DATA_X_OFLOW 0x40
74 #define PS2M_DATA_Y_SIGN 0x20
75 #define PS2M_DATA_X_SIGN 0x10
76 #define PS2M_DATA_AONE 0x08
77 #define PS2M_DATA_MID_BUTTON 0x04
78 #define PS2M_DATA_RIGHT_BUTTON 0x02
79 #define PS2M_DATA_LEFT_BUTTON 0x01
81 /* mouse status bits */
82 #define PS2M_STS_REMOTE_MODE 0x40
83 #define PS2M_STS_ENABLE_DEV 0x20
84 #define PS2M_STS_SCALING_21 0x10
85 #define PS2M_STS_MID_BUTTON 0x04
86 #define PS2M_STS_RIGHT_BUTTON 0x02
87 #define PS2M_STS_LEFT_BUTTON 0x01
89 #define PS2MOUSE_FIFOSZ 16
92 uint8_t buf[PS2MOUSE_FIFOSZ];
93 int rindex; /* index to read from */
94 int windex; /* index to write to */
95 int num; /* number of bytes in the fifo */
96 int size; /* size of the fifo */
99 struct ps2mouse_softc {
100 struct atkbdc_softc *atkbdc_sc;
105 uint8_t sampling_rate;
109 uint8_t curcmd; /* current command for next byte */
112 int delta_x, delta_y;
116 fifo_init(struct ps2mouse_softc *sc)
121 fifo->size = sizeof(((struct fifo *)0)->buf);
125 fifo_reset(struct ps2mouse_softc *sc)
130 bzero(fifo, sizeof(struct fifo));
131 fifo->size = sizeof(((struct fifo *)0)->buf);
135 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
140 if (fifo->num < fifo->size) {
141 fifo->buf[fifo->windex] = val;
142 fifo->windex = (fifo->windex + 1) % fifo->size;
148 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
154 *val = fifo->buf[fifo->rindex];
155 fifo->rindex = (fifo->rindex + 1) % fifo->size;
164 movement_reset(struct ps2mouse_softc *sc)
166 assert(pthread_mutex_isowned_np(&sc->mtx));
173 movement_update(struct ps2mouse_softc *sc, int x, int y)
175 sc->delta_x += x - sc->cur_x;
176 sc->delta_y += sc->cur_y - y;
182 movement_get(struct ps2mouse_softc *sc)
184 uint8_t val0, val1, val2;
186 assert(pthread_mutex_isowned_np(&sc->mtx));
188 val0 = PS2M_DATA_AONE;
189 val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
190 PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
192 if (sc->delta_x >= 0) {
193 if (sc->delta_x > 255) {
194 val0 |= PS2M_DATA_X_OFLOW;
199 val0 |= PS2M_DATA_X_SIGN;
200 if (sc->delta_x < -255) {
201 val0 |= PS2M_DATA_X_OFLOW;
208 if (sc->delta_y >= 0) {
209 if (sc->delta_y > 255) {
210 val0 |= PS2M_DATA_Y_OFLOW;
215 val0 |= PS2M_DATA_Y_SIGN;
216 if (sc->delta_y < -255) {
217 val0 |= PS2M_DATA_Y_OFLOW;
224 if (sc->fifo.num < (sc->fifo.size - 3)) {
232 ps2mouse_reset(struct ps2mouse_softc *sc)
234 assert(pthread_mutex_isowned_np(&sc->mtx));
237 sc->status = PS2M_STS_ENABLE_DEV;
239 sc->sampling_rate = 100;
248 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
252 pthread_mutex_lock(&sc->mtx);
253 retval = fifo_get(sc, val);
254 pthread_mutex_unlock(&sc->mtx);
260 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
262 return (sc->fifo.num);
266 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
268 pthread_mutex_lock(&sc->mtx);
277 pthread_mutex_unlock(&sc->mtx);
281 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
283 pthread_mutex_lock(&sc->mtx);
286 switch (sc->curcmd) {
287 case PS2MC_SET_SAMPLING_RATE:
288 sc->sampling_rate = val;
289 fifo_put(sc, PS2MC_ACK);
291 case PS2MC_SET_RESOLUTION:
292 sc->resolution = val;
293 fifo_put(sc, PS2MC_ACK);
296 EPRINTLN("Unhandled ps2 mouse current "
297 "command byte 0x%02x", val);
307 fifo_put(sc, PS2MC_ACK);
309 case PS2MC_RESET_DEV:
311 fifo_put(sc, PS2MC_ACK);
312 fifo_put(sc, PS2MC_BAT_SUCCESS);
313 fifo_put(sc, PS2MOUSE_DEV_ID);
315 case PS2MC_SET_DEFAULTS:
317 fifo_put(sc, PS2MC_ACK);
321 sc->status &= ~PS2M_STS_ENABLE_DEV;
322 fifo_put(sc, PS2MC_ACK);
326 sc->status |= PS2M_STS_ENABLE_DEV;
327 fifo_put(sc, PS2MC_ACK);
329 case PS2MC_SET_SAMPLING_RATE:
331 fifo_put(sc, PS2MC_ACK);
333 case PS2MC_SEND_DEV_ID:
334 fifo_put(sc, PS2MC_ACK);
335 fifo_put(sc, PS2MOUSE_DEV_ID);
337 case PS2MC_SET_REMOTE_MODE:
338 sc->status |= PS2M_STS_REMOTE_MODE;
339 fifo_put(sc, PS2MC_ACK);
341 case PS2MC_SEND_DEV_DATA:
342 fifo_put(sc, PS2MC_ACK);
345 case PS2MC_SET_STREAM_MODE:
346 sc->status &= ~PS2M_STS_REMOTE_MODE;
347 fifo_put(sc, PS2MC_ACK);
349 case PS2MC_SEND_DEV_STATUS:
350 fifo_put(sc, PS2MC_ACK);
351 fifo_put(sc, sc->status);
352 fifo_put(sc, sc->resolution);
353 fifo_put(sc, sc->sampling_rate);
355 case PS2MC_SET_RESOLUTION:
357 fifo_put(sc, PS2MC_ACK);
359 case PS2MC_SET_SCALING1:
360 case PS2MC_SET_SCALING2:
361 fifo_put(sc, PS2MC_ACK);
364 fifo_put(sc, PS2MC_ACK);
365 EPRINTLN("Unhandled ps2 mouse command "
370 pthread_mutex_unlock(&sc->mtx);
374 ps2mouse_event(uint8_t button, int x, int y, void *arg)
376 struct ps2mouse_softc *sc = arg;
378 pthread_mutex_lock(&sc->mtx);
379 movement_update(sc, x, y);
381 sc->status &= ~(PS2M_STS_LEFT_BUTTON |
382 PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
383 if (button & (1 << 0))
384 sc->status |= PS2M_STS_LEFT_BUTTON;
385 if (button & (1 << 1))
386 sc->status |= PS2M_STS_MID_BUTTON;
387 if (button & (1 << 2))
388 sc->status |= PS2M_STS_RIGHT_BUTTON;
390 if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
391 /* no data reporting */
392 pthread_mutex_unlock(&sc->mtx);
397 pthread_mutex_unlock(&sc->mtx);
399 if (sc->fifo.num > 0)
400 atkbdc_event(sc->atkbdc_sc, 0);
403 struct ps2mouse_softc *
404 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
406 struct ps2mouse_softc *sc;
408 sc = calloc(1, sizeof (struct ps2mouse_softc));
409 pthread_mutex_init(&sc->mtx, NULL);
411 sc->atkbdc_sc = atkbdc_sc;
413 pthread_mutex_lock(&sc->mtx);
415 pthread_mutex_unlock(&sc->mtx);
417 console_ptr_register(ps2mouse_event, sc, 1);
422 #ifdef BHYVE_SNAPSHOT
424 ps2mouse_snapshot(struct ps2mouse_softc *sc, struct vm_snapshot_meta *meta)
428 SNAPSHOT_VAR_OR_LEAVE(sc->status, meta, ret, done);
429 SNAPSHOT_VAR_OR_LEAVE(sc->resolution, meta, ret, done);
430 SNAPSHOT_VAR_OR_LEAVE(sc->sampling_rate, meta, ret, done);
431 SNAPSHOT_VAR_OR_LEAVE(sc->ctrlenable, meta, ret, done);
432 SNAPSHOT_VAR_OR_LEAVE(sc->curcmd, meta, ret, done);
433 SNAPSHOT_VAR_OR_LEAVE(sc->cur_x, meta, ret, done);
434 SNAPSHOT_VAR_OR_LEAVE(sc->cur_y, meta, ret, done);
435 SNAPSHOT_VAR_OR_LEAVE(sc->delta_x, meta, ret, done);
436 SNAPSHOT_VAR_OR_LEAVE(sc->delta_y, meta, ret, done);