]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/ps2mouse.c
Update to bmake-20171028
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / ps2mouse.c
1 /*-
2  * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3  * Copyright (c) 2015 Nahanni Systems Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/types.h>
32
33 #include <assert.h>
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <pthread.h>
39 #include <pthread_np.h>
40
41 #include "atkbdc.h"
42 #include "console.h"
43
44 /* mouse device commands */
45 #define PS2MC_RESET_DEV         0xff
46 #define PS2MC_SET_DEFAULTS      0xf6
47 #define PS2MC_DISABLE           0xf5
48 #define PS2MC_ENABLE            0xf4
49 #define PS2MC_SET_SAMPLING_RATE 0xf3
50 #define PS2MC_SEND_DEV_ID       0xf2
51 #define PS2MC_SET_REMOTE_MODE   0xf0
52 #define PS2MC_SEND_DEV_DATA     0xeb
53 #define PS2MC_SET_STREAM_MODE   0xea
54 #define PS2MC_SEND_DEV_STATUS   0xe9
55 #define PS2MC_SET_RESOLUTION    0xe8
56 #define PS2MC_SET_SCALING1      0xe7
57 #define PS2MC_SET_SCALING2      0xe6
58
59 #define PS2MC_BAT_SUCCESS       0xaa
60 #define PS2MC_ACK               0xfa
61
62 /* mouse device id */
63 #define PS2MOUSE_DEV_ID         0x0
64
65 /* mouse data bits */
66 #define PS2M_DATA_Y_OFLOW       0x80
67 #define PS2M_DATA_X_OFLOW       0x40
68 #define PS2M_DATA_Y_SIGN        0x20
69 #define PS2M_DATA_X_SIGN        0x10
70 #define PS2M_DATA_AONE          0x08
71 #define PS2M_DATA_MID_BUTTON    0x04
72 #define PS2M_DATA_RIGHT_BUTTON  0x02
73 #define PS2M_DATA_LEFT_BUTTON   0x01
74
75 /* mouse status bits */
76 #define PS2M_STS_REMOTE_MODE    0x40
77 #define PS2M_STS_ENABLE_DEV     0x20
78 #define PS2M_STS_SCALING_21     0x10
79 #define PS2M_STS_MID_BUTTON     0x04
80 #define PS2M_STS_RIGHT_BUTTON   0x02
81 #define PS2M_STS_LEFT_BUTTON    0x01
82
83 #define PS2MOUSE_FIFOSZ         16
84
85 struct fifo {
86         uint8_t buf[PS2MOUSE_FIFOSZ];
87         int     rindex;         /* index to read from */
88         int     windex;         /* index to write to */
89         int     num;            /* number of bytes in the fifo */
90         int     size;           /* size of the fifo */
91 };
92
93 struct ps2mouse_softc {
94         struct atkbdc_softc     *atkbdc_sc;
95         pthread_mutex_t         mtx;
96
97         uint8_t         status;
98         uint8_t         resolution;
99         uint8_t         sampling_rate;
100         int             ctrlenable;
101         struct fifo     fifo;
102
103         uint8_t         curcmd; /* current command for next byte */
104
105         int             cur_x, cur_y;
106         int             delta_x, delta_y;
107 };
108
109 static void
110 fifo_init(struct ps2mouse_softc *sc)
111 {
112         struct fifo *fifo;
113
114         fifo = &sc->fifo;
115         fifo->size = sizeof(((struct fifo *)0)->buf);
116 }
117
118 static void
119 fifo_reset(struct ps2mouse_softc *sc)
120 {
121         struct fifo *fifo;
122
123         fifo = &sc->fifo;
124         bzero(fifo, sizeof(struct fifo));
125         fifo->size = sizeof(((struct fifo *)0)->buf);
126 }
127
128 static void
129 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
130 {
131         struct fifo *fifo;
132
133         fifo = &sc->fifo;
134         if (fifo->num < fifo->size) {
135                 fifo->buf[fifo->windex] = val;
136                 fifo->windex = (fifo->windex + 1) % fifo->size;
137                 fifo->num++;
138         }
139 }
140
141 static int
142 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
143 {
144         struct fifo *fifo;
145
146         fifo = &sc->fifo;
147         if (fifo->num > 0) {
148                 *val = fifo->buf[fifo->rindex];
149                 fifo->rindex = (fifo->rindex + 1) % fifo->size;
150                 fifo->num--;
151                 return (0);
152         }
153
154         return (-1);
155 }
156
157 static void
158 movement_reset(struct ps2mouse_softc *sc)
159 {
160         assert(pthread_mutex_isowned_np(&sc->mtx));
161
162         sc->delta_x = 0;
163         sc->delta_y = 0;
164 }
165
166 static void
167 movement_update(struct ps2mouse_softc *sc, int x, int y)
168 {
169         sc->delta_x += x - sc->cur_x;
170         sc->delta_y += sc->cur_y - y;
171         sc->cur_x = x;
172         sc->cur_y = y;
173 }
174
175 static void
176 movement_get(struct ps2mouse_softc *sc)
177 {
178         uint8_t val0, val1, val2;
179
180         assert(pthread_mutex_isowned_np(&sc->mtx));
181
182         val0 = PS2M_DATA_AONE;
183         val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
184             PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
185
186         if (sc->delta_x >= 0) {
187                 if (sc->delta_x > 255) {
188                         val0 |= PS2M_DATA_X_OFLOW;
189                         val1 = 255;
190                 } else
191                         val1 = sc->delta_x;
192         } else {
193                 val0 |= PS2M_DATA_X_SIGN;
194                 if (sc->delta_x < -255) {
195                         val0 |= PS2M_DATA_X_OFLOW;
196                         val1 = 255;
197                 } else
198                         val1 = sc->delta_x;
199         }
200         sc->delta_x = 0;
201
202         if (sc->delta_y >= 0) {
203                 if (sc->delta_y > 255) {
204                         val0 |= PS2M_DATA_Y_OFLOW;
205                         val2 = 255;
206                 } else
207                         val2 = sc->delta_y;
208         } else {
209                 val0 |= PS2M_DATA_Y_SIGN;
210                 if (sc->delta_y < -255) {
211                         val0 |= PS2M_DATA_Y_OFLOW;
212                         val2 = 255;
213                 } else
214                         val2 = sc->delta_y;
215         }
216         sc->delta_y = 0;
217
218         if (sc->fifo.num < (sc->fifo.size - 3)) {
219                 fifo_put(sc, val0);
220                 fifo_put(sc, val1);
221                 fifo_put(sc, val2);
222         }
223 }
224
225 static void
226 ps2mouse_reset(struct ps2mouse_softc *sc)
227 {
228         assert(pthread_mutex_isowned_np(&sc->mtx));
229         fifo_reset(sc);
230         movement_reset(sc);
231         sc->status = PS2M_STS_ENABLE_DEV;
232         sc->resolution = 4;
233         sc->sampling_rate = 100;
234
235         sc->cur_x = 0;
236         sc->cur_y = 0;
237         sc->delta_x = 0;
238         sc->delta_y = 0;
239 }
240
241 int
242 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
243 {
244         int retval;
245
246         pthread_mutex_lock(&sc->mtx);
247         retval = fifo_get(sc, val);
248         pthread_mutex_unlock(&sc->mtx);
249
250         return (retval);
251 }
252
253 int
254 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
255 {
256         return (sc->fifo.num);
257 }
258
259 void
260 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
261 {
262         pthread_mutex_lock(&sc->mtx);
263         if (enable)
264                 sc->ctrlenable = 1;
265         else {
266                 sc->ctrlenable = 0;
267                 sc->fifo.rindex = 0;
268                 sc->fifo.windex = 0;
269                 sc->fifo.num = 0;
270         }
271         pthread_mutex_unlock(&sc->mtx);
272 }
273
274 void
275 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
276 {
277         pthread_mutex_lock(&sc->mtx);
278         fifo_reset(sc);
279         if (sc->curcmd) {
280                 switch (sc->curcmd) {
281                 case PS2MC_SET_SAMPLING_RATE:
282                         sc->sampling_rate = val;
283                         fifo_put(sc, PS2MC_ACK);
284                         break;
285                 case PS2MC_SET_RESOLUTION:
286                         sc->resolution = val;
287                         fifo_put(sc, PS2MC_ACK);
288                         break;
289                 default:
290                         fprintf(stderr, "Unhandled ps2 mouse current "
291                             "command byte 0x%02x\n", val);
292                         break;
293                 }
294                 sc->curcmd = 0;
295
296         } else if (insert) {
297                 fifo_put(sc, val);
298         } else {
299                 switch (val) {
300                 case 0x00:
301                         fifo_put(sc, PS2MC_ACK);
302                         break;
303                 case PS2MC_RESET_DEV:
304                         ps2mouse_reset(sc);
305                         fifo_put(sc, PS2MC_ACK);
306                         fifo_put(sc, PS2MC_BAT_SUCCESS);
307                         fifo_put(sc, PS2MOUSE_DEV_ID);
308                         break;
309                 case PS2MC_SET_DEFAULTS:
310                         ps2mouse_reset(sc);
311                         fifo_put(sc, PS2MC_ACK);
312                         break;
313                 case PS2MC_DISABLE:
314                         fifo_reset(sc);
315                         sc->status &= ~PS2M_STS_ENABLE_DEV;
316                         fifo_put(sc, PS2MC_ACK);
317                         break;
318                 case PS2MC_ENABLE:
319                         fifo_reset(sc);
320                         sc->status |= PS2M_STS_ENABLE_DEV;
321                         fifo_put(sc, PS2MC_ACK);
322                         break;
323                 case PS2MC_SET_SAMPLING_RATE:
324                         sc->curcmd = val;
325                         fifo_put(sc, PS2MC_ACK);
326                         break;
327                 case PS2MC_SEND_DEV_ID:
328                         fifo_put(sc, PS2MC_ACK);
329                         fifo_put(sc, PS2MOUSE_DEV_ID);
330                         break;
331                 case PS2MC_SET_REMOTE_MODE:
332                         sc->status |= PS2M_STS_REMOTE_MODE;
333                         fifo_put(sc, PS2MC_ACK);
334                         break;
335                 case PS2MC_SEND_DEV_DATA:
336                         fifo_put(sc, PS2MC_ACK);
337                         movement_get(sc);
338                         break;
339                 case PS2MC_SET_STREAM_MODE:
340                         sc->status &= ~PS2M_STS_REMOTE_MODE;
341                         fifo_put(sc, PS2MC_ACK);
342                         break;
343                 case PS2MC_SEND_DEV_STATUS:
344                         fifo_put(sc, PS2MC_ACK);
345                         fifo_put(sc, sc->status);
346                         fifo_put(sc, sc->resolution);
347                         fifo_put(sc, sc->sampling_rate);
348                         break;
349                 case PS2MC_SET_RESOLUTION:
350                         sc->curcmd = val;
351                         fifo_put(sc, PS2MC_ACK);
352                         break;
353                 case PS2MC_SET_SCALING1:
354                 case PS2MC_SET_SCALING2:
355                         fifo_put(sc, PS2MC_ACK);
356                         break;
357                 default:
358                         fifo_put(sc, PS2MC_ACK);
359                         fprintf(stderr, "Unhandled ps2 mouse command "
360                             "0x%02x\n", val);
361                         break;
362                 }
363         }
364         pthread_mutex_unlock(&sc->mtx);
365 }
366
367 static void
368 ps2mouse_event(uint8_t button, int x, int y, void *arg)
369 {
370         struct ps2mouse_softc *sc = arg;
371
372         pthread_mutex_lock(&sc->mtx);
373         movement_update(sc, x, y);
374
375         sc->status &= ~(PS2M_STS_LEFT_BUTTON |
376             PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
377         if (button & (1 << 0))
378                 sc->status |= PS2M_STS_LEFT_BUTTON;
379         if (button & (1 << 1))
380                 sc->status |= PS2M_STS_MID_BUTTON;
381         if (button & (1 << 2))
382                 sc->status |= PS2M_STS_RIGHT_BUTTON;
383
384         if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
385                 /* no data reporting */
386                 pthread_mutex_unlock(&sc->mtx);
387                 return;
388         }
389
390         movement_get(sc);
391         pthread_mutex_unlock(&sc->mtx);
392
393         if (sc->fifo.num > 0)
394                 atkbdc_event(sc->atkbdc_sc, 0);
395 }
396
397 struct ps2mouse_softc *
398 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
399 {
400         struct ps2mouse_softc *sc;
401
402         sc = calloc(1, sizeof (struct ps2mouse_softc));
403         pthread_mutex_init(&sc->mtx, NULL);
404         fifo_init(sc);
405         sc->atkbdc_sc = atkbdc_sc;
406
407         pthread_mutex_lock(&sc->mtx);
408         ps2mouse_reset(sc);
409         pthread_mutex_unlock(&sc->mtx);
410
411         console_ptr_register(ps2mouse_event, sc, 1);
412
413         return (sc);
414 }
415
416