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>
51 /* mouse device commands */
52 #define PS2MC_RESET_DEV 0xff
53 #define PS2MC_SET_DEFAULTS 0xf6
54 #define PS2MC_DISABLE 0xf5
55 #define PS2MC_ENABLE 0xf4
56 #define PS2MC_SET_SAMPLING_RATE 0xf3
57 #define PS2MC_SEND_DEV_ID 0xf2
58 #define PS2MC_SET_REMOTE_MODE 0xf0
59 #define PS2MC_SEND_DEV_DATA 0xeb
60 #define PS2MC_SET_STREAM_MODE 0xea
61 #define PS2MC_SEND_DEV_STATUS 0xe9
62 #define PS2MC_SET_RESOLUTION 0xe8
63 #define PS2MC_SET_SCALING1 0xe7
64 #define PS2MC_SET_SCALING2 0xe6
66 #define PS2MC_BAT_SUCCESS 0xaa
67 #define PS2MC_ACK 0xfa
70 #define PS2MOUSE_DEV_ID 0x0
73 #define PS2M_DATA_Y_OFLOW 0x80
74 #define PS2M_DATA_X_OFLOW 0x40
75 #define PS2M_DATA_Y_SIGN 0x20
76 #define PS2M_DATA_X_SIGN 0x10
77 #define PS2M_DATA_AONE 0x08
78 #define PS2M_DATA_MID_BUTTON 0x04
79 #define PS2M_DATA_RIGHT_BUTTON 0x02
80 #define PS2M_DATA_LEFT_BUTTON 0x01
82 /* mouse status bits */
83 #define PS2M_STS_REMOTE_MODE 0x40
84 #define PS2M_STS_ENABLE_DEV 0x20
85 #define PS2M_STS_SCALING_21 0x10
86 #define PS2M_STS_MID_BUTTON 0x04
87 #define PS2M_STS_RIGHT_BUTTON 0x02
88 #define PS2M_STS_LEFT_BUTTON 0x01
90 #define PS2MOUSE_FIFOSZ 16
93 uint8_t buf[PS2MOUSE_FIFOSZ];
94 int rindex; /* index to read from */
95 int windex; /* index to write to */
96 int num; /* number of bytes in the fifo */
97 int size; /* size of the fifo */
100 struct ps2mouse_softc {
101 struct atkbdc_softc *atkbdc_sc;
106 uint8_t sampling_rate;
110 uint8_t curcmd; /* current command for next byte */
113 int delta_x, delta_y;
117 fifo_init(struct ps2mouse_softc *sc)
122 fifo->size = sizeof(((struct fifo *)0)->buf);
126 fifo_reset(struct ps2mouse_softc *sc)
131 bzero(fifo, sizeof(struct fifo));
132 fifo->size = sizeof(((struct fifo *)0)->buf);
136 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
141 if (fifo->num < fifo->size) {
142 fifo->buf[fifo->windex] = val;
143 fifo->windex = (fifo->windex + 1) % fifo->size;
149 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
155 *val = fifo->buf[fifo->rindex];
156 fifo->rindex = (fifo->rindex + 1) % fifo->size;
165 movement_reset(struct ps2mouse_softc *sc)
167 assert(pthread_mutex_isowned_np(&sc->mtx));
174 movement_update(struct ps2mouse_softc *sc, int x, int y)
176 sc->delta_x += x - sc->cur_x;
177 sc->delta_y += sc->cur_y - y;
183 movement_get(struct ps2mouse_softc *sc)
185 uint8_t val0, val1, val2;
187 assert(pthread_mutex_isowned_np(&sc->mtx));
189 val0 = PS2M_DATA_AONE;
190 val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
191 PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
193 if (sc->delta_x >= 0) {
194 if (sc->delta_x > 255) {
195 val0 |= PS2M_DATA_X_OFLOW;
200 val0 |= PS2M_DATA_X_SIGN;
201 if (sc->delta_x < -255) {
202 val0 |= PS2M_DATA_X_OFLOW;
209 if (sc->delta_y >= 0) {
210 if (sc->delta_y > 255) {
211 val0 |= PS2M_DATA_Y_OFLOW;
216 val0 |= PS2M_DATA_Y_SIGN;
217 if (sc->delta_y < -255) {
218 val0 |= PS2M_DATA_Y_OFLOW;
225 if (sc->fifo.num < (sc->fifo.size - 3)) {
233 ps2mouse_reset(struct ps2mouse_softc *sc)
235 assert(pthread_mutex_isowned_np(&sc->mtx));
238 sc->status = PS2M_STS_ENABLE_DEV;
240 sc->sampling_rate = 100;
249 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
253 pthread_mutex_lock(&sc->mtx);
254 retval = fifo_get(sc, val);
255 pthread_mutex_unlock(&sc->mtx);
261 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
263 return (sc->fifo.num);
267 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
269 pthread_mutex_lock(&sc->mtx);
278 pthread_mutex_unlock(&sc->mtx);
282 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
284 pthread_mutex_lock(&sc->mtx);
287 switch (sc->curcmd) {
288 case PS2MC_SET_SAMPLING_RATE:
289 sc->sampling_rate = val;
290 fifo_put(sc, PS2MC_ACK);
292 case PS2MC_SET_RESOLUTION:
293 sc->resolution = val;
294 fifo_put(sc, PS2MC_ACK);
297 EPRINTLN("Unhandled ps2 mouse current "
298 "command byte 0x%02x", val);
308 fifo_put(sc, PS2MC_ACK);
310 case PS2MC_RESET_DEV:
312 fifo_put(sc, PS2MC_ACK);
313 fifo_put(sc, PS2MC_BAT_SUCCESS);
314 fifo_put(sc, PS2MOUSE_DEV_ID);
316 case PS2MC_SET_DEFAULTS:
318 fifo_put(sc, PS2MC_ACK);
322 sc->status &= ~PS2M_STS_ENABLE_DEV;
323 fifo_put(sc, PS2MC_ACK);
327 sc->status |= PS2M_STS_ENABLE_DEV;
328 fifo_put(sc, PS2MC_ACK);
330 case PS2MC_SET_SAMPLING_RATE:
332 fifo_put(sc, PS2MC_ACK);
334 case PS2MC_SEND_DEV_ID:
335 fifo_put(sc, PS2MC_ACK);
336 fifo_put(sc, PS2MOUSE_DEV_ID);
338 case PS2MC_SET_REMOTE_MODE:
339 sc->status |= PS2M_STS_REMOTE_MODE;
340 fifo_put(sc, PS2MC_ACK);
342 case PS2MC_SEND_DEV_DATA:
343 fifo_put(sc, PS2MC_ACK);
346 case PS2MC_SET_STREAM_MODE:
347 sc->status &= ~PS2M_STS_REMOTE_MODE;
348 fifo_put(sc, PS2MC_ACK);
350 case PS2MC_SEND_DEV_STATUS:
351 fifo_put(sc, PS2MC_ACK);
352 fifo_put(sc, sc->status);
353 fifo_put(sc, sc->resolution);
354 fifo_put(sc, sc->sampling_rate);
356 case PS2MC_SET_RESOLUTION:
358 fifo_put(sc, PS2MC_ACK);
360 case PS2MC_SET_SCALING1:
361 case PS2MC_SET_SCALING2:
362 fifo_put(sc, PS2MC_ACK);
365 fifo_put(sc, PS2MC_ACK);
366 EPRINTLN("Unhandled ps2 mouse command "
371 pthread_mutex_unlock(&sc->mtx);
375 ps2mouse_event(uint8_t button, int x, int y, void *arg)
377 struct ps2mouse_softc *sc = arg;
379 pthread_mutex_lock(&sc->mtx);
380 movement_update(sc, x, y);
382 sc->status &= ~(PS2M_STS_LEFT_BUTTON |
383 PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
384 if (button & (1 << 0))
385 sc->status |= PS2M_STS_LEFT_BUTTON;
386 if (button & (1 << 1))
387 sc->status |= PS2M_STS_MID_BUTTON;
388 if (button & (1 << 2))
389 sc->status |= PS2M_STS_RIGHT_BUTTON;
391 if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
392 /* no data reporting */
393 pthread_mutex_unlock(&sc->mtx);
398 pthread_mutex_unlock(&sc->mtx);
400 if (sc->fifo.num > 0)
401 atkbdc_event(sc->atkbdc_sc, 0);
404 struct ps2mouse_softc *
405 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
407 struct ps2mouse_softc *sc;
409 sc = calloc(1, sizeof (struct ps2mouse_softc));
410 pthread_mutex_init(&sc->mtx, NULL);
412 sc->atkbdc_sc = atkbdc_sc;
414 pthread_mutex_lock(&sc->mtx);
416 pthread_mutex_unlock(&sc->mtx);
418 console_ptr_register(ps2mouse_event, sc, 1);
423 #ifdef BHYVE_SNAPSHOT
425 ps2mouse_snapshot(struct ps2mouse_softc *sc, struct vm_snapshot_meta *meta)
429 SNAPSHOT_VAR_OR_LEAVE(sc->status, meta, ret, done);
430 SNAPSHOT_VAR_OR_LEAVE(sc->resolution, meta, ret, done);
431 SNAPSHOT_VAR_OR_LEAVE(sc->sampling_rate, meta, ret, done);
432 SNAPSHOT_VAR_OR_LEAVE(sc->ctrlenable, meta, ret, done);
433 SNAPSHOT_VAR_OR_LEAVE(sc->curcmd, meta, ret, done);
434 SNAPSHOT_VAR_OR_LEAVE(sc->cur_x, meta, ret, done);
435 SNAPSHOT_VAR_OR_LEAVE(sc->cur_y, meta, ret, done);
436 SNAPSHOT_VAR_OR_LEAVE(sc->delta_x, meta, ret, done);
437 SNAPSHOT_VAR_OR_LEAVE(sc->delta_y, meta, ret, done);