1 | /*
|
---|
2 | * Copyright (c) 2011 Martin Decky
|
---|
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_proto
|
---|
30 | * @ingroup input
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief PS/2 protocol driver.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <mouse.h>
|
---|
39 | #include <mouse_port.h>
|
---|
40 | #include <mouse_proto.h>
|
---|
41 |
|
---|
42 | #define PS2_MOUSE_OUT_INIT 0xf4
|
---|
43 | #define PS2_MOUSE_ACK 0xfa
|
---|
44 |
|
---|
45 | #define BUFSIZE 3
|
---|
46 |
|
---|
47 | typedef struct {
|
---|
48 | union {
|
---|
49 | unsigned char data[BUFSIZE];
|
---|
50 | struct {
|
---|
51 | unsigned int leftbtn : 1;
|
---|
52 | unsigned int rightbtn : 1;
|
---|
53 | unsigned int middlebtn : 1;
|
---|
54 | unsigned int isone : 1; /* Always one */
|
---|
55 | unsigned int xsign : 1;
|
---|
56 | unsigned int ysign : 1;
|
---|
57 | unsigned int xovfl : 1;
|
---|
58 | unsigned int 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 unsigned int bufpos;
|
---|
67 | static unsigned int leftbtn;
|
---|
68 | static unsigned int rightbtn;
|
---|
69 | static unsigned int middlebtn;
|
---|
70 |
|
---|
71 | static mouse_dev_t *mouse_dev;
|
---|
72 |
|
---|
73 | static int ps2_proto_init(mouse_dev_t *mdev)
|
---|
74 | {
|
---|
75 | mouse_dev = mdev;
|
---|
76 | bufpos = 0;
|
---|
77 | leftbtn = 0;
|
---|
78 | rightbtn = 0;
|
---|
79 |
|
---|
80 | mouse_dev->port_ops->write(PS2_MOUSE_OUT_INIT);
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Convert 9-bit 2-complement signed number to integer */
|
---|
85 | static int bit9toint(int sign, unsigned char data)
|
---|
86 | {
|
---|
87 | int tmp;
|
---|
88 |
|
---|
89 | if (!sign)
|
---|
90 | return data;
|
---|
91 |
|
---|
92 | tmp = ((unsigned char) ~data) + 1;
|
---|
93 | return -tmp;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Process mouse data */
|
---|
97 | static void ps2_proto_parse(sysarg_t data)
|
---|
98 | {
|
---|
99 | int x, y;
|
---|
100 |
|
---|
101 | /* Check that we have not lost synchronization */
|
---|
102 | if (bufpos == 0 && !(data & 0x8))
|
---|
103 | return; /* Synchro lost, ignore byte */
|
---|
104 |
|
---|
105 | buf.u.data[bufpos++] = data;
|
---|
106 | if (bufpos == BUFSIZE) {
|
---|
107 | bufpos = 0;
|
---|
108 |
|
---|
109 | if (buf.u.val.leftbtn ^ leftbtn) {
|
---|
110 | leftbtn = buf.u.val.leftbtn;
|
---|
111 | mouse_push_event_button(mouse_dev, 1, leftbtn);
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (buf.u.val.rightbtn ^ rightbtn) {
|
---|
115 | rightbtn = buf.u.val.rightbtn;
|
---|
116 | mouse_push_event_button(mouse_dev, 2, rightbtn);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (buf.u.val.middlebtn ^ middlebtn) {
|
---|
120 | middlebtn = buf.u.val.middlebtn;
|
---|
121 | mouse_push_event_button(mouse_dev, 3, middlebtn);
|
---|
122 | }
|
---|
123 |
|
---|
124 | x = bit9toint(buf.u.val.xsign, buf.u.val.x);
|
---|
125 | y = -bit9toint(buf.u.val.ysign, buf.u.val.y);
|
---|
126 |
|
---|
127 | if (x != 0 || y != 0)
|
---|
128 | mouse_push_event_move(mouse_dev, x, y);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | mouse_proto_ops_t ps2_proto = {
|
---|
133 | .parse = ps2_proto_parse,
|
---|
134 | .init = ps2_proto_init
|
---|
135 | };
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @}
|
---|
139 | */
|
---|