summaryrefslogtreecommitdiff
path: root/driver/mouse.c
blob: 787c7a6cd54cc86340972d2133039f7454a57153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//https://wiki.osdev.org/PS/2_Mouse
//TODO: ignore last packet for 4 packets mouse?

#include <stdint.h>

#include "mouse.h"

#include "ringbuffer.h"
#include "compositor.h"
#include "interrupts.h"
#include "kernel/kernel.h"
#include "log.h"
#include "asm_x86.h"

static volatile uint8_t mouse_cycle;
static volatile uint8_t mouse_byte[3];

// data stream coming from the mouse will be waiting inside this buffer
static ringbuffer mouse_in;

// called on each interrupt (keep it small) 
// just pushes mouse data into the buffer
uint32_t mouse_interrupt(uint32_t esp)
{
    if(!ringbuffer_put(&mouse_in,x86_inb(0x60)))kpanic("full");
    return esp;
}

// process data in buffer (return true if anything was processed)
bool mouse_worker()
{
    if(ringbuffer_full(&mouse_in))
    {
        kpanic("mouse buffer full"); // heavy debugging
    }
    bool wake=false;
    while(ringbuffer_has(&mouse_in)){
        mouse_handler(ringbuffer_get(&mouse_in));
        wake=true;
    }
    return wake;
}

//
void mouse_wait(uint8_t a_type) //unsigned char
{
  uint32_t _time_out=100000; //unsigned int
  if(a_type==0)
  {
    while(_time_out--) //Data
    {
      if((x86_inb(0x64) & 1)==1)
      {
        return;
      }
    }
    return;
  }
  else
  {
    while(_time_out--) //Signal
    {
      if((x86_inb(0x64) & 2)==0)
      {
        return;
      }
    }
    return;
  }
}

static uint8_t mouse_read()
{
  //Get's response from mouse
  mouse_wait(0); 
  return x86_inb(0x60);
}

static void mouse_write(uint8_t  a_write) 
{
  //Wait to be able to send a command
  mouse_wait(1);
  //Tell the mouse we are sending a command
  x86_outb(0x64, 0xD4);
  //Wait for the final part
  mouse_wait(1);
  //Finally write
  x86_outb(0x60, a_write);
}


void mouse_init()
{

  mouse_in=ringbuffer_init(1);// 4096 bytes ringbuffer;

  uint8_t _status;  //unsigned char

  //Enable the auxiliary mouse device
  mouse_wait(1);
  x86_outb(0x64, 0xA8);
  
  //Enable the interrupts
  mouse_wait(1);
  x86_outb(0x64, 0x20);
  mouse_wait(0);
  _status=(x86_inb(0x60) | 2);
  mouse_wait(1);
  x86_outb(0x64, 0x60);
  mouse_wait(1);
  x86_outb(0x60, _status);
  
  //Tell the mouse to use default settings
  mouse_write(0xF6);
  mouse_read();  //Acknowledge
  
  //Enable the mouse
  mouse_write(0xF4);
  mouse_read();  //Acknowledge
  
  interrupt_register(INTERRUPT_MOUSE,&mouse_interrupt);
}

// called every time after the 3 bytes have been filled.
void mouse_action()
{
    /*     3rd byte bits have this meaning:
     *
     * key 0    left button         1
     * key 1    right button        2
     * key 2    middle button       4
     * key 3    always one!         8
     *
     * key 4    x-axis sign bit    16 
     * key 5    y-axis sign bit    32
     *
     * key 6    x-axis overflow    64
     * key 7    y-axis overflow   128
     */

    int rel_x=0;
    int rel_y=0;

    /* ignore overflows? or assume max? */

    if(mouse_byte[0]&0x80)
    {
#ifdef LOG_MOUSE_WARN
        klog("x overflow");
#endif
        return;
    }

    if(mouse_byte[0]&0x40)
    {
#ifdef LOG_MOUSE_WARN
        klog("y overflow");
#endif
        return;
    }

    rel_x = mouse_byte[1] - ((mouse_byte[0] << 4) & 0x100);
    rel_y = mouse_byte[2] - ((mouse_byte[0] << 3) & 0x100);
    
    // we pass the state through
    compositor_mouse_handle(rel_x,-rel_y,mouse_byte[0]);
}

void mouse_handler(uint8_t byte)//struct regs *a_r) //struct regs *a_r (not used but just there)
{
//    X86_IRQ_BEGIN

  switch(mouse_cycle)
  {
    case 0:
      mouse_byte[0]=byte;
      mouse_cycle++;

    // this bit is ALWAYS one, otherwise we are out of sync!
    if(!(mouse_byte[0]&0x8))
    {
#ifdef LOG_MOUSE_WARN
        klog("mouse out of sync");
#endif
        mouse_cycle--;
    }

      break;
    case 1:
      mouse_byte[1]=byte;
      mouse_cycle++;
      break;
    case 2:
      mouse_byte[2]=byte;
      mouse_cycle=0;
      mouse_action();
      break;
  }

//  X86_IRQ_END
}