00001 /* 00002 * Copyright (c) 2006 Ondrej Palkovsky 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * - Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * - The name of the author may not be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00037 #include <stdio.h> 00038 #include <mouse_port.h> 00039 #include <char_mouse.h> 00040 #include <mouse_proto.h> 00041 00042 #define BUFSIZE 3 00043 00044 #define PS2_MOUSE_OUT_INIT 0xf4 00045 #define PS2_MOUSE_ACK 0xfa 00046 00047 typedef struct { 00048 union { 00049 unsigned char data[BUFSIZE]; 00050 struct { 00051 unsigned leftbtn : 1; 00052 unsigned rightbtn : 1; 00053 unsigned middlebtn : 1; 00054 unsigned isone : 1; /* Always one */ 00055 unsigned xsign : 1; 00056 unsigned ysign : 1; 00057 unsigned xovfl : 1; 00058 unsigned yovfl : 1; 00059 unsigned char x; 00060 unsigned char y; 00061 } val; 00062 } u; 00063 } ps2packet_t; 00064 00065 static ps2packet_t buf; 00066 static int bufpos = 0; 00067 static int leftbtn = 0; 00068 static int rightbtn = 0; 00069 static int middlebtn = 0; 00070 00071 int mouse_proto_init(void) 00072 { 00073 mouse_port_write(PS2_MOUSE_OUT_INIT); 00074 return 0; 00075 } 00076 00078 static int bit9toint(int sign, unsigned char data) 00079 { 00080 int tmp; 00081 00082 if (!sign) 00083 return data; 00084 00085 tmp = ((unsigned char)~data) + 1; 00086 return -tmp; 00087 } 00088 00090 void mouse_proto_parse_byte(int data) 00091 { 00092 int x, y; 00093 00094 /* Check that we have not lost synchronization */ 00095 if (bufpos == 0 && !(data & 0x8)) 00096 return; /* Synchro lost, ignore byte */ 00097 00098 buf.u.data[bufpos++] = data; 00099 if (bufpos == BUFSIZE) { 00100 bufpos = 0; 00101 00102 if (buf.u.val.leftbtn ^ leftbtn) { 00103 leftbtn = buf.u.val.leftbtn; 00104 mouse_ev_btn(1, leftbtn); 00105 } 00106 00107 if (buf.u.val.rightbtn ^ rightbtn) { 00108 rightbtn = buf.u.val.rightbtn; 00109 mouse_ev_btn(2, rightbtn); 00110 } 00111 00112 if (buf.u.val.middlebtn ^ middlebtn) { 00113 middlebtn = buf.u.val.middlebtn; 00114 mouse_ev_btn(3, middlebtn); 00115 } 00116 00117 x = bit9toint(buf.u.val.xsign, buf.u.val.x); 00118 y = - bit9toint(buf.u.val.ysign, buf.u.val.y); 00119 00120 if (x != 0 || y != 0) { 00121 mouse_ev_move(x, y); 00122 } 00123 } 00124 00125 return; 00126 } 00127