| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup mouse
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief PS/2 mouse protocol driver.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <mouse_port.h>
|
|---|
| 39 | #include <char_mouse.h>
|
|---|
| 40 | #include <mouse_proto.h>
|
|---|
| 41 |
|
|---|
| 42 | #define BUFSIZE 3
|
|---|
| 43 |
|
|---|
| 44 | #define PS2_MOUSE_OUT_INIT 0xf4
|
|---|
| 45 | #define PS2_MOUSE_ACK 0xfa
|
|---|
| 46 |
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | union {
|
|---|
| 49 | unsigned char data[BUFSIZE];
|
|---|
| 50 | struct {
|
|---|
| 51 | unsigned leftbtn : 1;
|
|---|
| 52 | unsigned rightbtn : 1;
|
|---|
| 53 | unsigned middlebtn : 1;
|
|---|
| 54 | unsigned isone : 1; /* Always one */
|
|---|
| 55 | unsigned xsign : 1;
|
|---|
| 56 | unsigned ysign : 1;
|
|---|
| 57 | unsigned xovfl : 1;
|
|---|
| 58 | unsigned yovfl : 1;
|
|---|
| 59 | unsigned char x;
|
|---|
| 60 | unsigned char y;
|
|---|
| 61 | } val;
|
|---|
| 62 | } u;
|
|---|
| 63 | } ps2packet_t;
|
|---|
| 64 |
|
|---|
| 65 | static ps2packet_t buf;
|
|---|
| 66 | static int bufpos = 0;
|
|---|
| 67 | static int leftbtn = 0;
|
|---|
| 68 | static int rightbtn = 0;
|
|---|
| 69 | static int middlebtn = 0;
|
|---|
| 70 |
|
|---|
| 71 | int mouse_proto_init(void)
|
|---|
| 72 | {
|
|---|
| 73 | mouse_port_write(PS2_MOUSE_OUT_INIT);
|
|---|
| 74 | return 0;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Convert 9-bit 2-complement signed number to integer */
|
|---|
| 78 | static int bit9toint(int sign, unsigned char data)
|
|---|
| 79 | {
|
|---|
| 80 | int tmp;
|
|---|
| 81 |
|
|---|
| 82 | if (!sign)
|
|---|
| 83 | return data;
|
|---|
| 84 |
|
|---|
| 85 | tmp = ((unsigned char)~data) + 1;
|
|---|
| 86 | return -tmp;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /** Process mouse data */
|
|---|
| 90 | void mouse_proto_parse_byte(int data)
|
|---|
| 91 | {
|
|---|
| 92 | int x, y;
|
|---|
| 93 |
|
|---|
| 94 | /* Check that we have not lost synchronization */
|
|---|
| 95 | if (bufpos == 0 && !(data & 0x8))
|
|---|
| 96 | return; /* Synchro lost, ignore byte */
|
|---|
| 97 |
|
|---|
| 98 | buf.u.data[bufpos++] = data;
|
|---|
| 99 | if (bufpos == BUFSIZE) {
|
|---|
| 100 | bufpos = 0;
|
|---|
| 101 |
|
|---|
| 102 | if (buf.u.val.leftbtn ^ leftbtn) {
|
|---|
| 103 | leftbtn = buf.u.val.leftbtn;
|
|---|
| 104 | mouse_ev_btn(1, leftbtn);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (buf.u.val.rightbtn ^ rightbtn) {
|
|---|
| 108 | rightbtn = buf.u.val.rightbtn;
|
|---|
| 109 | mouse_ev_btn(2, rightbtn);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (buf.u.val.middlebtn ^ middlebtn) {
|
|---|
| 113 | middlebtn = buf.u.val.middlebtn;
|
|---|
| 114 | mouse_ev_btn(3, middlebtn);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | x = bit9toint(buf.u.val.xsign, buf.u.val.x);
|
|---|
| 118 | y = - bit9toint(buf.u.val.ysign, buf.u.val.y);
|
|---|
| 119 |
|
|---|
| 120 | if (x != 0 || y != 0) {
|
|---|
| 121 | mouse_ev_move(x, y);
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * @}
|
|---|
| 130 | */
|
|---|