UbixOS  2.0
mouse.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <isa/mouse.h>
30 #include <isa/8259.h>
31 #include <sys/idt.h>
32 #include <sys/gdt.h>
33 #include <sys/io.h>
34 #include <lib/kprintf.h>
35 
36 static uInt8 kbdRead() {
37  unsigned long Timeout;
38  uInt8 Stat, Data;
39 
40  for (Timeout = 50000L; Timeout != 0; Timeout--) {
41  Stat = inportByte(0x64);
42 
43  /* loop until 8042 output buffer full */
44  if ((Stat & 0x01) != 0) {
45  Data = inportByte(0x60);
46 
47  /* loop if parity error or receive timeout */
48  if ((Stat & 0xC0) == 0)
49  return Data;
50  }
51  }
52  return -1;
53 }
54 
55 static void kbdWrite(uInt16 port, uInt8 data) {
56  uInt32 timeout;
57  uInt8 stat;
58 
59  for (timeout = 500000L; timeout != 0; timeout--) {
60  stat = inportByte(0x64);
61 
62  if ((stat & 0x02) == 0)
63  break;
64  }
65 
66  if (timeout != 0)
67  outportByte(port, data);
68 }
69 
70 static uInt8 kbdWriteRead(uInt16 port, uInt8 data, const char* expect) {
71  int RetVal;
72 
73  kbdWrite(port, data);
74  for (; *expect; expect++) {
75  RetVal = kbdRead();
76  if ((uInt8) *expect != RetVal) {
77  return RetVal;
78  }
79  }
80 
81  return 0;
82 }
83 
84 int mouseInit() {
85  static uInt8 s1[] = { 0xF3, 0xC8, 0xF3, 0x64, 0xF3, 0x50, 0 };
86  static uInt8 s2[] = { 0xF6, 0xE6, 0xF4, 0xF3, 0x64, 0xE8, 0x03, 0 };
87  const uInt8* ch;
88  Int8 cmd = 0x0;
89 
90  kbdWrite(0x64, 0xA8);
91  for (ch = s1; *ch; ch++) {
92  kbdWrite(0x64, 0xD4);
93  kbdWriteRead(0x60, *ch, "\xFA");
94  }
95  for (ch = s2; *ch; ch++) {
96  kbdWrite(0x64, 0xD4);
97  kbdWriteRead(0x60, *ch, "\xFA");
98  }
99  kbdWrite(0x64, 0xD4);
100  if (kbdWriteRead(0x60, 0xF2, "\xFA") != 0x0) {
101  kprintf("Error With Mouse\n");
102  }
103  cmd = kbdRead();
104  kprintf("CMD: [0x%X]\n", cmd);
105  kbdWrite(0x64, 0xD4);
106  kbdWriteRead(0x60, 0xF4, "\xFA");
107 
108  setVector(&mouseISR, mVec + 12, dPresent + dInt + dDpl3);
109 
110  outportByte( mPic, eoi);
111  outportByte( sPic, eoi);
112  irqEnable(12);
113  outportByte( mPic, eoi);
114  outportByte( sPic, eoi);
115 
116  kprintf("psm0 - Address: [0x%X]\n", &mouseISR);
117 
118  /* Return so we know everything went well */
119  return (0x0);
120 }
121 
122 asm(
123  ".globl mouseISR \n"
124  "mouseISR: \n"
125  " pusha \n" /* Save all registers */
126  " call mouseHandler \n"
127  " popa \n"
128  " iret \n" /* Exit interrupt */
129 );
130 
131 void mouseHandler() {
132  kprintf("MOUSE!!!\n");
133 
134  outportByte( mPic, eoi);
135  outportByte( sPic, eoi);
136  /* Return */
137  return;
138 }
139 
140 /***
141  $Log: mouse.c,v $
142  Revision 1.1.1.1 2006/06/01 12:46:12 reddawg
143  ubix2
144 
145  Revision 1.2 2005/10/12 00:13:37 reddawg
146  Removed
147 
148  Revision 1.1.1.1 2005/09/26 17:24:02 reddawg
149  no message
150 
151  Revision 1.3 2004/09/07 21:54:38 reddawg
152  ok reverted back to old scheduling for now....
153 
154  Revision 1.2 2004/09/06 15:13:25 reddawg
155  Last commit before FreeBSD 6.0
156 
157  Revision 1.1 2004/06/04 10:20:53 reddawg
158  mouse drive: fixed a few bugs works a bit better now
159 
160  END
161  ***/
162 
8259.h
stat
Definition: stat.h:44
gdt.h
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
dPresent
#define dPresent
Definition: gdt.h:54
Int8
signed char Int8
Definition: objgfx30.h:43
idt.h
uInt16
unsigned short int uInt16
Definition: objgfx30.h:48
outportByte
void outportByte(unsigned int, unsigned char)
outputut one byte to specified port
Definition: io.c:72
inportByte
unsigned char inportByte(unsigned int)
input one byte from specified port
Definition: io.c:38
mouseHandler
void mouseHandler()
Definition: mouse.c:131
kprintf.h
uInt8
unsigned char uInt8
Definition: objgfx30.h:47
mouse.h
eoi
#define eoi
Definition: 8259.h:38
sPic
#define sPic
Definition: 8259.h:36
dInt
#define dInt
Definition: gdt.h:43
mVec
#define mVec
Definition: 8259.h:41
setVector
void setVector(void *handler, unsigned char interrupt, unsigned short controlMajor)
Definition: idt.c:208
io.h
irqEnable
void irqEnable(uInt16 irqNo)
L
#define L
Definition: types.h:155
mouseISR
void mouseISR()
mouseInit
int mouseInit()
Definition: mouse.c:84
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
mPic
#define mPic
Definition: 8259.h:34
dDpl3
#define dDpl3
Definition: gdt.h:50