]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/ps2mouse.c
Merge llvm-project release/15.x llvmorg-15.0.7-0-g8dfdcc7b7bf6
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / ps2mouse.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5  * Copyright (c) 2015 Nahanni Systems Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34
35 #include <machine/vmm_snapshot.h>
36
37 #include <assert.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <pthread.h>
44 #include <pthread_np.h>
45
46 #include "atkbdc.h"
47 #include "console.h"
48 #include "debug.h"
49 #include "ps2mouse.h"
50
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
65
66 #define PS2MC_BAT_SUCCESS       0xaa
67 #define PS2MC_ACK               0xfa
68
69 /* mouse device id */
70 #define PS2MOUSE_DEV_ID         0x0
71
72 /* mouse data bits */
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
81
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
89
90 #define PS2MOUSE_FIFOSZ         16
91
92 struct fifo {
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 */
98 };
99
100 struct ps2mouse_softc {
101         struct atkbdc_softc     *atkbdc_sc;
102         pthread_mutex_t         mtx;
103
104         uint8_t         status;
105         uint8_t         resolution;
106         uint8_t         sampling_rate;
107         int             ctrlenable;
108         struct fifo     fifo;
109
110         uint8_t         curcmd; /* current command for next byte */
111
112         int             cur_x, cur_y;
113         int             delta_x, delta_y;
114 };
115
116 static void
117 fifo_init(struct ps2mouse_softc *sc)
118 {
119         struct fifo *fifo;
120
121         fifo = &sc->fifo;
122         fifo->size = sizeof(((struct fifo *)0)->buf);
123 }
124
125 static void
126 fifo_reset(struct ps2mouse_softc *sc)
127 {
128         struct fifo *fifo;
129
130         fifo = &sc->fifo;
131         bzero(fifo, sizeof(struct fifo));
132         fifo->size = sizeof(((struct fifo *)0)->buf);
133 }
134
135 static void
136 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
137 {
138         struct fifo *fifo;
139
140         fifo = &sc->fifo;
141         if (fifo->num < fifo->size) {
142                 fifo->buf[fifo->windex] = val;
143                 fifo->windex = (fifo->windex + 1) % fifo->size;
144                 fifo->num++;
145         }
146 }
147
148 static int
149 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
150 {
151         struct fifo *fifo;
152
153         fifo = &sc->fifo;
154         if (fifo->num > 0) {
155                 *val = fifo->buf[fifo->rindex];
156                 fifo->rindex = (fifo->rindex + 1) % fifo->size;
157                 fifo->num--;
158                 return (0);
159         }
160
161         return (-1);
162 }
163
164 static void
165 movement_reset(struct ps2mouse_softc *sc)
166 {
167         assert(pthread_mutex_isowned_np(&sc->mtx));
168
169         sc->delta_x = 0;
170         sc->delta_y = 0;
171 }
172
173 static void
174 movement_update(struct ps2mouse_softc *sc, int x, int y)
175 {
176         sc->delta_x += x - sc->cur_x;
177         sc->delta_y += sc->cur_y - y;
178         sc->cur_x = x;
179         sc->cur_y = y;
180 }
181
182 static void
183 movement_get(struct ps2mouse_softc *sc)
184 {
185         uint8_t val0, val1, val2;
186
187         assert(pthread_mutex_isowned_np(&sc->mtx));
188
189         val0 = PS2M_DATA_AONE;
190         val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
191             PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
192
193         if (sc->delta_x >= 0) {
194                 if (sc->delta_x > 255) {
195                         val0 |= PS2M_DATA_X_OFLOW;
196                         val1 = 255;
197                 } else
198                         val1 = sc->delta_x;
199         } else {
200                 val0 |= PS2M_DATA_X_SIGN;
201                 if (sc->delta_x < -255) {
202                         val0 |= PS2M_DATA_X_OFLOW;
203                         val1 = 255;
204                 } else
205                         val1 = sc->delta_x;
206         }
207         sc->delta_x = 0;
208
209         if (sc->delta_y >= 0) {
210                 if (sc->delta_y > 255) {
211                         val0 |= PS2M_DATA_Y_OFLOW;
212                         val2 = 255;
213                 } else
214                         val2 = sc->delta_y;
215         } else {
216                 val0 |= PS2M_DATA_Y_SIGN;
217                 if (sc->delta_y < -255) {
218                         val0 |= PS2M_DATA_Y_OFLOW;
219                         val2 = 255;
220                 } else
221                         val2 = sc->delta_y;
222         }
223         sc->delta_y = 0;
224
225         if (sc->fifo.num < (sc->fifo.size - 3)) {
226                 fifo_put(sc, val0);
227                 fifo_put(sc, val1);
228                 fifo_put(sc, val2);
229         }
230 }
231
232 static void
233 ps2mouse_reset(struct ps2mouse_softc *sc)
234 {
235         assert(pthread_mutex_isowned_np(&sc->mtx));
236         fifo_reset(sc);
237         movement_reset(sc);
238         sc->status = PS2M_STS_ENABLE_DEV;
239         sc->resolution = 4;
240         sc->sampling_rate = 100;
241
242         sc->cur_x = 0;
243         sc->cur_y = 0;
244         sc->delta_x = 0;
245         sc->delta_y = 0;
246 }
247
248 int
249 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
250 {
251         int retval;
252
253         pthread_mutex_lock(&sc->mtx);
254         retval = fifo_get(sc, val);
255         pthread_mutex_unlock(&sc->mtx);
256
257         return (retval);
258 }
259
260 int
261 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
262 {
263         return (sc->fifo.num);
264 }
265
266 void
267 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
268 {
269         pthread_mutex_lock(&sc->mtx);
270         if (enable)
271                 sc->ctrlenable = 1;
272         else {
273                 sc->ctrlenable = 0;
274                 sc->fifo.rindex = 0;
275                 sc->fifo.windex = 0;
276                 sc->fifo.num = 0;
277         }
278         pthread_mutex_unlock(&sc->mtx);
279 }
280
281 void
282 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
283 {
284         pthread_mutex_lock(&sc->mtx);
285         fifo_reset(sc);
286         if (sc->curcmd) {
287                 switch (sc->curcmd) {
288                 case PS2MC_SET_SAMPLING_RATE:
289                         sc->sampling_rate = val;
290                         fifo_put(sc, PS2MC_ACK);
291                         break;
292                 case PS2MC_SET_RESOLUTION:
293                         sc->resolution = val;
294                         fifo_put(sc, PS2MC_ACK);
295                         break;
296                 default:
297                         EPRINTLN("Unhandled ps2 mouse current "
298                             "command byte 0x%02x", val);
299                         break;
300                 }
301                 sc->curcmd = 0;
302
303         } else if (insert) {
304                 fifo_put(sc, val);
305         } else {
306                 switch (val) {
307                 case 0x00:
308                         fifo_put(sc, PS2MC_ACK);
309                         break;
310                 case PS2MC_RESET_DEV:
311                         ps2mouse_reset(sc);
312                         fifo_put(sc, PS2MC_ACK);
313                         fifo_put(sc, PS2MC_BAT_SUCCESS);
314                         fifo_put(sc, PS2MOUSE_DEV_ID);
315                         break;
316                 case PS2MC_SET_DEFAULTS:
317                         ps2mouse_reset(sc);
318                         fifo_put(sc, PS2MC_ACK);
319                         break;
320                 case PS2MC_DISABLE:
321                         fifo_reset(sc);
322                         sc->status &= ~PS2M_STS_ENABLE_DEV;
323                         fifo_put(sc, PS2MC_ACK);
324                         break;
325                 case PS2MC_ENABLE:
326                         fifo_reset(sc);
327                         sc->status |= PS2M_STS_ENABLE_DEV;
328                         fifo_put(sc, PS2MC_ACK);
329                         break;
330                 case PS2MC_SET_SAMPLING_RATE:
331                         sc->curcmd = val;
332                         fifo_put(sc, PS2MC_ACK);
333                         break;
334                 case PS2MC_SEND_DEV_ID:
335                         fifo_put(sc, PS2MC_ACK);
336                         fifo_put(sc, PS2MOUSE_DEV_ID);
337                         break;
338                 case PS2MC_SET_REMOTE_MODE:
339                         sc->status |= PS2M_STS_REMOTE_MODE;
340                         fifo_put(sc, PS2MC_ACK);
341                         break;
342                 case PS2MC_SEND_DEV_DATA:
343                         fifo_put(sc, PS2MC_ACK);
344                         movement_get(sc);
345                         break;
346                 case PS2MC_SET_STREAM_MODE:
347                         sc->status &= ~PS2M_STS_REMOTE_MODE;
348                         fifo_put(sc, PS2MC_ACK);
349                         break;
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);
355                         break;
356                 case PS2MC_SET_RESOLUTION:
357                         sc->curcmd = val;
358                         fifo_put(sc, PS2MC_ACK);
359                         break;
360                 case PS2MC_SET_SCALING1:
361                 case PS2MC_SET_SCALING2:
362                         fifo_put(sc, PS2MC_ACK);
363                         break;
364                 default:
365                         fifo_put(sc, PS2MC_ACK);
366                         EPRINTLN("Unhandled ps2 mouse command "
367                             "0x%02x", val);
368                         break;
369                 }
370         }
371         pthread_mutex_unlock(&sc->mtx);
372 }
373
374 static void
375 ps2mouse_event(uint8_t button, int x, int y, void *arg)
376 {
377         struct ps2mouse_softc *sc = arg;
378
379         pthread_mutex_lock(&sc->mtx);
380         movement_update(sc, x, y);
381
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;
390
391         if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
392                 /* no data reporting */
393                 pthread_mutex_unlock(&sc->mtx);
394                 return;
395         }
396
397         movement_get(sc);
398         pthread_mutex_unlock(&sc->mtx);
399
400         if (sc->fifo.num > 0)
401                 atkbdc_event(sc->atkbdc_sc, 0);
402 }
403
404 struct ps2mouse_softc *
405 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
406 {
407         struct ps2mouse_softc *sc;
408
409         sc = calloc(1, sizeof (struct ps2mouse_softc));
410         pthread_mutex_init(&sc->mtx, NULL);
411         fifo_init(sc);
412         sc->atkbdc_sc = atkbdc_sc;
413
414         pthread_mutex_lock(&sc->mtx);
415         ps2mouse_reset(sc);
416         pthread_mutex_unlock(&sc->mtx);
417
418         console_ptr_register(ps2mouse_event, sc, 1);
419
420         return (sc);
421 }
422
423 #ifdef BHYVE_SNAPSHOT
424 int
425 ps2mouse_snapshot(struct ps2mouse_softc *sc, struct vm_snapshot_meta *meta)
426 {
427         int ret;
428
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);
438
439 done:
440         return (ret);
441 }
442 #endif