source: mainline/uspace/srv/hid/char_mouse/proto/ps2.c@ a70bda4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a70bda4 was b73c26d, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Add ADB mouse driver.

  • Property mode set to 100644
File size: 3.1 KB
Line 
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_proto.h>
39#include <char_mouse.h>
40
41#define BUFSIZE 3
42
43typedef struct {
44 union {
45 unsigned char data[BUFSIZE];
46 struct {
47 unsigned leftbtn : 1;
48 unsigned rightbtn : 1;
49 unsigned middlebtn : 1;
50 unsigned isone : 1; /* Always one */
51 unsigned xsign : 1;
52 unsigned ysign : 1;
53 unsigned xovfl : 1;
54 unsigned yovfl : 1;
55 unsigned char x;
56 unsigned char y;
57 } val;
58 } u;
59} ps2packet_t;
60
61static ps2packet_t buf;
62static int bufpos = 0;
63static int leftbtn = 0;
64static int rightbtn = 0;
65static int middlebtn = 0;
66
67int mouse_proto_init(void)
68{
69 return 0;
70}
71
72/** Convert 9-bit 2-complement signed number to integer */
73static int bit9toint(int sign, unsigned char data)
74{
75 int tmp;
76
77 if (!sign)
78 return data;
79
80 tmp = ((unsigned char)~data) + 1;
81 return -tmp;
82}
83
84/** Process mouse data */
85void mouse_proto_parse_byte(int data)
86{
87 int x, y;
88
89 /* Check that we have not lost synchronization */
90 if (bufpos == 0 && !(data & 0x8))
91 return; /* Synchro lost, ignore byte */
92
93 buf.u.data[bufpos++] = data;
94 if (bufpos == BUFSIZE) {
95 bufpos = 0;
96
97 if (buf.u.val.leftbtn ^ leftbtn) {
98 leftbtn = buf.u.val.leftbtn;
99 mouse_ev_btn(1, leftbtn);
100 }
101
102 if (buf.u.val.rightbtn ^ rightbtn) {
103 rightbtn = buf.u.val.rightbtn;
104 mouse_ev_btn(2, rightbtn);
105 }
106
107 if (buf.u.val.middlebtn ^ middlebtn) {
108 middlebtn = buf.u.val.middlebtn;
109 mouse_ev_btn(3, middlebtn);
110 }
111
112 x = bit9toint(buf.u.val.xsign, buf.u.val.x);
113 y = - bit9toint(buf.u.val.ysign, buf.u.val.y);
114
115 if (x != 0 || y != 0) {
116 mouse_ev_move(x, y);
117 }
118 }
119
120 return;
121}
122
123/**
124 * @}
125 */
Note: See TracBrowser for help on using the repository browser.