2 * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3 * Copyright (c) 2015 Nahanni Systems Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
39 #include <pthread_np.h>
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
59 #define PS2MC_BAT_SUCCESS 0xaa
60 #define PS2MC_ACK 0xfa
63 #define PS2MOUSE_DEV_ID 0x0
65 /* mouse status bits */
66 #define PS2M_STS_REMOTE_MODE 0x40
67 #define PS2M_STS_ENABLE_DEV 0x20
68 #define PS2M_STS_SCALING_21 0x10
69 #define PS2M_STS_MID_BUTTON 0x04
70 #define PS2M_STS_RIGHT_BUTTON 0x02
71 #define PS2M_STS_LEFT_BUTTON 0x01
73 #define PS2MOUSE_FIFOSZ 16
76 uint8_t buf[PS2MOUSE_FIFOSZ];
77 int rindex; /* index to read from */
78 int windex; /* index to write to */
79 int num; /* number of bytes in the fifo */
80 int size; /* size of the fifo */
83 struct ps2mouse_softc {
84 struct atkbdc_softc *atkbdc_sc;
89 uint8_t sampling_rate;
93 uint8_t curcmd; /* current command for next byte */
100 fifo_init(struct ps2mouse_softc *sc)
105 fifo->size = sizeof(((struct fifo *)0)->buf);
109 fifo_reset(struct ps2mouse_softc *sc)
114 bzero(fifo, sizeof(struct fifo));
115 fifo->size = sizeof(((struct fifo *)0)->buf);
119 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
124 if (fifo->num < fifo->size) {
125 fifo->buf[fifo->windex] = val;
126 fifo->windex = (fifo->windex + 1) % fifo->size;
132 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
138 *val = fifo->buf[fifo->rindex];
139 fifo->rindex = (fifo->rindex + 1) % fifo->size;
148 movement_reset(struct ps2mouse_softc *sc)
150 assert(pthread_mutex_isowned_np(&sc->mtx));
157 movement_update(struct ps2mouse_softc *sc, int x, int y)
159 sc->delta_x += x - sc->cur_x;
160 sc->delta_y += sc->cur_y - y;
166 movement_get(struct ps2mouse_softc *sc)
168 uint8_t val0, val1, val2;
170 assert(pthread_mutex_isowned_np(&sc->mtx));
172 val0 = sc->status & (PS2M_STS_LEFT_BUTTON |
173 PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
175 if (sc->delta_x >= 0) {
176 if (sc->delta_x > 255) {
183 if (sc->delta_x < -255) {
191 if (sc->delta_y >= 0) {
192 if (sc->delta_y > 255) {
199 if (sc->delta_y < -255) {
207 if (sc->fifo.num < (sc->fifo.size - 3)) {
215 ps2mouse_reset(struct ps2mouse_softc *sc)
217 assert(pthread_mutex_isowned_np(&sc->mtx));
220 sc->status = PS2M_STS_ENABLE_DEV;
222 sc->sampling_rate = 100;
231 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
235 pthread_mutex_lock(&sc->mtx);
236 retval = fifo_get(sc, val);
237 pthread_mutex_unlock(&sc->mtx);
243 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
245 return (sc->fifo.num);
249 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
251 pthread_mutex_lock(&sc->mtx);
260 pthread_mutex_unlock(&sc->mtx);
264 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
266 pthread_mutex_lock(&sc->mtx);
269 switch (sc->curcmd) {
270 case PS2MC_SET_SAMPLING_RATE:
271 sc->sampling_rate = val;
272 fifo_put(sc, PS2MC_ACK);
274 case PS2MC_SET_RESOLUTION:
275 sc->resolution = val;
276 fifo_put(sc, PS2MC_ACK);
279 fprintf(stderr, "Unhandled ps2 mouse current "
280 "command byte 0x%02x\n", val);
290 fifo_put(sc, PS2MC_ACK);
292 case PS2MC_RESET_DEV:
294 fifo_put(sc, PS2MC_ACK);
295 fifo_put(sc, PS2MC_BAT_SUCCESS);
296 fifo_put(sc, PS2MOUSE_DEV_ID);
298 case PS2MC_SET_DEFAULTS:
300 fifo_put(sc, PS2MC_ACK);
304 sc->status &= ~PS2M_STS_ENABLE_DEV;
305 fifo_put(sc, PS2MC_ACK);
309 sc->status |= PS2M_STS_ENABLE_DEV;
310 fifo_put(sc, PS2MC_ACK);
312 case PS2MC_SET_SAMPLING_RATE:
314 fifo_put(sc, PS2MC_ACK);
316 case PS2MC_SEND_DEV_ID:
317 fifo_put(sc, PS2MC_ACK);
318 fifo_put(sc, PS2MOUSE_DEV_ID);
320 case PS2MC_SET_REMOTE_MODE:
321 sc->status |= PS2M_STS_REMOTE_MODE;
322 fifo_put(sc, PS2MC_ACK);
324 case PS2MC_SEND_DEV_DATA:
325 fifo_put(sc, PS2MC_ACK);
328 case PS2MC_SET_STREAM_MODE:
329 sc->status &= ~PS2M_STS_REMOTE_MODE;
330 fifo_put(sc, PS2MC_ACK);
332 case PS2MC_SEND_DEV_STATUS:
333 fifo_put(sc, PS2MC_ACK);
334 fifo_put(sc, sc->status);
335 fifo_put(sc, sc->resolution);
336 fifo_put(sc, sc->sampling_rate);
338 case PS2MC_SET_RESOLUTION:
340 fifo_put(sc, PS2MC_ACK);
342 case PS2MC_SET_SCALING1:
343 case PS2MC_SET_SCALING2:
344 fifo_put(sc, PS2MC_ACK);
347 fifo_put(sc, PS2MC_ACK);
348 fprintf(stderr, "Unhandled ps2 mouse command "
353 pthread_mutex_unlock(&sc->mtx);
357 ps2mouse_event(uint8_t button, int x, int y, void *arg)
359 struct ps2mouse_softc *sc = arg;
361 pthread_mutex_lock(&sc->mtx);
362 movement_update(sc, x, y);
364 sc->status &= ~(PS2M_STS_LEFT_BUTTON |
365 PS2M_STS_RIGHT_BUTTON | PS2M_STS_MID_BUTTON);
366 if (button & (1 << 0))
367 sc->status |= PS2M_STS_LEFT_BUTTON;
368 if (button & (1 << 1))
369 sc->status |= PS2M_STS_MID_BUTTON;
370 if (button & (1 << 2))
371 sc->status |= PS2M_STS_RIGHT_BUTTON;
373 if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
374 /* no data reporting */
375 pthread_mutex_unlock(&sc->mtx);
380 pthread_mutex_unlock(&sc->mtx);
382 if (sc->fifo.num > 0)
383 atkbdc_event(sc->atkbdc_sc, 0);
386 struct ps2mouse_softc *
387 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
389 struct ps2mouse_softc *sc;
391 sc = calloc(1, sizeof (struct ps2mouse_softc));
392 pthread_mutex_init(&sc->mtx, NULL);
394 sc->atkbdc_sc = atkbdc_sc;
396 pthread_mutex_lock(&sc->mtx);
398 pthread_mutex_unlock(&sc->mtx);
400 console_ptr_register(ps2mouse_event, sc, 1);