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
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
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
83 #define PS2MOUSE_FIFOSZ 16
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 */
93 struct ps2mouse_softc {
94 struct atkbdc_softc *atkbdc_sc;
99 uint8_t sampling_rate;
103 uint8_t curcmd; /* current command for next byte */
106 int delta_x, delta_y;
110 fifo_init(struct ps2mouse_softc *sc)
115 fifo->size = sizeof(((struct fifo *)0)->buf);
119 fifo_reset(struct ps2mouse_softc *sc)
124 bzero(fifo, sizeof(struct fifo));
125 fifo->size = sizeof(((struct fifo *)0)->buf);
129 fifo_put(struct ps2mouse_softc *sc, uint8_t val)
134 if (fifo->num < fifo->size) {
135 fifo->buf[fifo->windex] = val;
136 fifo->windex = (fifo->windex + 1) % fifo->size;
142 fifo_get(struct ps2mouse_softc *sc, uint8_t *val)
148 *val = fifo->buf[fifo->rindex];
149 fifo->rindex = (fifo->rindex + 1) % fifo->size;
158 movement_reset(struct ps2mouse_softc *sc)
160 assert(pthread_mutex_isowned_np(&sc->mtx));
167 movement_update(struct ps2mouse_softc *sc, int x, int y)
169 sc->delta_x += x - sc->cur_x;
170 sc->delta_y += sc->cur_y - y;
176 movement_get(struct ps2mouse_softc *sc)
178 uint8_t val0, val1, val2;
180 assert(pthread_mutex_isowned_np(&sc->mtx));
182 val0 = PS2M_DATA_AONE;
183 val0 |= sc->status & (PS2M_DATA_LEFT_BUTTON |
184 PS2M_DATA_RIGHT_BUTTON | PS2M_DATA_MID_BUTTON);
186 if (sc->delta_x >= 0) {
187 if (sc->delta_x > 255) {
188 val0 |= PS2M_DATA_X_OFLOW;
193 val0 |= PS2M_DATA_X_SIGN;
194 if (sc->delta_x < -255) {
195 val0 |= PS2M_DATA_X_OFLOW;
202 if (sc->delta_y >= 0) {
203 if (sc->delta_y > 255) {
204 val0 |= PS2M_DATA_Y_OFLOW;
209 val0 |= PS2M_DATA_Y_SIGN;
210 if (sc->delta_y < -255) {
211 val0 |= PS2M_DATA_Y_OFLOW;
218 if (sc->fifo.num < (sc->fifo.size - 3)) {
226 ps2mouse_reset(struct ps2mouse_softc *sc)
228 assert(pthread_mutex_isowned_np(&sc->mtx));
231 sc->status = PS2M_STS_ENABLE_DEV;
233 sc->sampling_rate = 100;
242 ps2mouse_read(struct ps2mouse_softc *sc, uint8_t *val)
246 pthread_mutex_lock(&sc->mtx);
247 retval = fifo_get(sc, val);
248 pthread_mutex_unlock(&sc->mtx);
254 ps2mouse_fifocnt(struct ps2mouse_softc *sc)
256 return (sc->fifo.num);
260 ps2mouse_toggle(struct ps2mouse_softc *sc, int enable)
262 pthread_mutex_lock(&sc->mtx);
271 pthread_mutex_unlock(&sc->mtx);
275 ps2mouse_write(struct ps2mouse_softc *sc, uint8_t val, int insert)
277 pthread_mutex_lock(&sc->mtx);
280 switch (sc->curcmd) {
281 case PS2MC_SET_SAMPLING_RATE:
282 sc->sampling_rate = val;
283 fifo_put(sc, PS2MC_ACK);
285 case PS2MC_SET_RESOLUTION:
286 sc->resolution = val;
287 fifo_put(sc, PS2MC_ACK);
290 fprintf(stderr, "Unhandled ps2 mouse current "
291 "command byte 0x%02x\n", val);
301 fifo_put(sc, PS2MC_ACK);
303 case PS2MC_RESET_DEV:
305 fifo_put(sc, PS2MC_ACK);
306 fifo_put(sc, PS2MC_BAT_SUCCESS);
307 fifo_put(sc, PS2MOUSE_DEV_ID);
309 case PS2MC_SET_DEFAULTS:
311 fifo_put(sc, PS2MC_ACK);
315 sc->status &= ~PS2M_STS_ENABLE_DEV;
316 fifo_put(sc, PS2MC_ACK);
320 sc->status |= PS2M_STS_ENABLE_DEV;
321 fifo_put(sc, PS2MC_ACK);
323 case PS2MC_SET_SAMPLING_RATE:
325 fifo_put(sc, PS2MC_ACK);
327 case PS2MC_SEND_DEV_ID:
328 fifo_put(sc, PS2MC_ACK);
329 fifo_put(sc, PS2MOUSE_DEV_ID);
331 case PS2MC_SET_REMOTE_MODE:
332 sc->status |= PS2M_STS_REMOTE_MODE;
333 fifo_put(sc, PS2MC_ACK);
335 case PS2MC_SEND_DEV_DATA:
336 fifo_put(sc, PS2MC_ACK);
339 case PS2MC_SET_STREAM_MODE:
340 sc->status &= ~PS2M_STS_REMOTE_MODE;
341 fifo_put(sc, PS2MC_ACK);
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);
349 case PS2MC_SET_RESOLUTION:
351 fifo_put(sc, PS2MC_ACK);
353 case PS2MC_SET_SCALING1:
354 case PS2MC_SET_SCALING2:
355 fifo_put(sc, PS2MC_ACK);
358 fifo_put(sc, PS2MC_ACK);
359 fprintf(stderr, "Unhandled ps2 mouse command "
364 pthread_mutex_unlock(&sc->mtx);
368 ps2mouse_event(uint8_t button, int x, int y, void *arg)
370 struct ps2mouse_softc *sc = arg;
372 pthread_mutex_lock(&sc->mtx);
373 movement_update(sc, x, y);
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;
384 if ((sc->status & PS2M_STS_ENABLE_DEV) == 0 || !sc->ctrlenable) {
385 /* no data reporting */
386 pthread_mutex_unlock(&sc->mtx);
391 pthread_mutex_unlock(&sc->mtx);
393 if (sc->fifo.num > 0)
394 atkbdc_event(sc->atkbdc_sc, 0);
397 struct ps2mouse_softc *
398 ps2mouse_init(struct atkbdc_softc *atkbdc_sc)
400 struct ps2mouse_softc *sc;
402 sc = calloc(1, sizeof (struct ps2mouse_softc));
403 pthread_mutex_init(&sc->mtx, NULL);
405 sc->atkbdc_sc = atkbdc_sc;
407 pthread_mutex_lock(&sc->mtx);
409 pthread_mutex_unlock(&sc->mtx);
411 console_ptr_register(ps2mouse_event, sc, 1);