Subversion Repositories Kolibri OS

Rev

Rev 3539 | Rev 5075 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
5072 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2014. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;; Includes source code by Kulakov Vladimir Gennadievich.       ;;
7
;; Modified by Mario79 and Rus.                                 ;;
8
;; 02.12.2009                                              ;;
9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
 
5072 hidnplayr 11
format PE DLL native
12
entry START
2288 clevermous 13
 
5072 hidnplayr 14
        CURRENT_API             = 0x0200
15
        COMPATIBLE_API          = 0x0100
16
        API_VERSION             = (COMPATIBLE_API shl 16) + CURRENT_API
2288 clevermous 17
 
5072 hidnplayr 18
        __DEBUG__               = 1
19
        __DEBUG_LEVEL__         = 2
2288 clevermous 20
 
5072 hidnplayr 21
section '.flat' readable writable executable
2288 clevermous 22
 
5072 hidnplayr 23
include '../proc32.inc'
24
include '../struct.inc'
25
include '../macros.inc'
26
include '../fdo.inc'
2288 clevermous 27
 
5072 hidnplayr 28
; Serial data packet format:
29
;            D6    D5    D4    D3    D2    D1    D0
30
; 1st byte    1    LB    RB    Y7    Y6    X7    X6
31
; 2nd byte    0    X5    X4    X3    X2    X1    X0
32
; 3rd byte    0    Y5    Y4    Y3    Y2    Y1    Y0
2288 clevermous 33
 
5072 hidnplayr 34
struct com_mouse_data
2288 clevermous 35
 
5072 hidnplayr 36
        port            dw ?
37
        offset          db ?
38
        data            rb 3
2288 clevermous 39
 
5072 hidnplayr 40
ends
2288 clevermous 41
 
5072 hidnplayr 42
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43
;;                        ;;
44
;; proc START             ;;
45
;;                        ;;
46
;; (standard driver proc) ;;
47
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2288 clevermous 48
 
5072 hidnplayr 49
proc START c, reason:dword, cmdline:dword
2288 clevermous 50
 
5072 hidnplayr 51
        cmp     [reason], DRV_ENTRY
52
        jne     .fail
2288 clevermous 53
 
5072 hidnplayr 54
        DEBUGF  2,"Loading serial mouse driver\n"
2288 clevermous 55
 
5072 hidnplayr 56
        stdcall init_mouse, 0x3f8, 4
57
        stdcall init_mouse, 0x2f8, 3
58
        stdcall init_mouse, 0x3e8, 4
59
        stdcall init_mouse, 0x2e8, 3
2288 clevermous 60
 
5072 hidnplayr 61
        invoke  RegService, my_service, service_proc
62
        ret
2288 clevermous 63
 
5072 hidnplayr 64
  .fail:
2288 clevermous 65
        xor     eax, eax
66
        ret
5072 hidnplayr 67
 
2288 clevermous 68
endp
69
 
70
 
71
proc service_proc stdcall, ioctl:dword
72
 
73
        mov     ebx, [ioctl]
5072 hidnplayr 74
        mov     eax, [ebx + IOCTL.io_code]
75
        cmp     eax, 0 ;SRV_GETVERSION
76
        jne     .fail
2288 clevermous 77
 
5072 hidnplayr 78
        mov     eax, [ebx + IOCTL.output]
79
        cmp     [ebx + IOCTL.out_size], 4
2288 clevermous 80
        jne     .fail
81
        mov     [eax], dword API_VERSION
82
        xor     eax, eax
83
        ret
5072 hidnplayr 84
 
85
  .fail:
2288 clevermous 86
        or      eax, -1
87
        ret
88
endp
89
 
5072 hidnplayr 90
 
91
proc init_mouse stdcall port, irq
92
 
93
        DEBUGF  1, "Trying to init serial mouse on port 0x%x\n", [port]
94
 
95
        xor     ebx, ebx        ; reserve port area
96
        mov     ecx, [port]
97
        lea     edx, [ecx + 7]
98
        push    ebp
99
        invoke  ReservePortArea
100
        pop     ebp
101
        test    eax, eax
102
        jnz     .fail
103
 
104
        DEBUGF  1, "Reserved port area\n"
105
 
106
        mov     bx, word[port]
107
 
108
        ; Set the speed to 1200 baud
2288 clevermous 109
        mov     dx, bx
5072 hidnplayr 110
        add     dx, 3
111
        in      al, dx
112
        or      al, 80h         ; set DLAB bit
113
        out     dx, al
114
 
115
        mov     dx, bx
116
        mov     al, 60h         ; 1200 baud
117
        out     dx, al
2288 clevermous 118
        inc     dx
5072 hidnplayr 119
        mov     al, 0
120
        out     dx, al
121
 
122
        ; Use 7 bit words, 1 stop bit, no parity control, reset DLAB bit
123
        mov     dx, bx
124
        add     dx, 3
125
        mov     al, 00000010b
126
        out     dx, al
127
 
128
        ; Disable interrupts
129
        mov     dx, bx
130
        inc     dx
131
        mov     al, 0
132
        out     dx, al
133
 
134
; Check if a MS type serial mouse is connected
135
 
136
; Disable power and mouse interrupts
137
        mov     dx, bx
138
        add     dx, 4           ; modem control register
139
        mov     al, 0           ; reset DTR, RTS, and OUT2
140
        out     dx, al
141
 
142
; Wait 5 ticks (0.2s)
143
        mov     esi, 200
144
        invoke  Sleep
145
 
146
; Power on the mouse
2288 clevermous 147
        mov     al, 1
148
        out     dx, al
5072 hidnplayr 149
 
150
; Wait 5 ticks (0.2s)
151
        mov     esi, 200
152
        invoke  Sleep
153
 
154
; Clear data register
2288 clevermous 155
        mov     dx, bx
5072 hidnplayr 156
        in      al, dx
157
 
158
; set DTR, DTS and OUT2
159
        add     dx, 4
160
        mov     al, 1011b
161
        out     dx, al
162
 
2288 clevermous 163
        mov     ecx, 0x1FFFF
5072 hidnplayr 164
; Poll port
165
  .loop:
2288 clevermous 166
        dec     ecx
5072 hidnplayr 167
        jz      .fail
168
 
169
; Check if identification byte is available
170
        mov     dx, bx
171
        add     dx, 5
172
        in      al, dx
173
        test    al, 1           ; data ready?
174
        jz      .loop
175
 
176
; Read data byte
177
        mov     dx, bx
178
        in      al, dx
179
        cmp     al, 'M'
180
        jne     .free
181
 
182
        DEBUGF  2, "Serial mouse detected on port 0x%x\n", [port]
183
 
184
; Create data struct
185
 
186
        invoke  Kmalloc, sizeof.com_mouse_data
187
        test    eax, eax
188
        jz      .fail
189
 
190
        DEBUGF  1, "Structure 0x%x allocated\n", eax
191
 
192
        mov     bx, word[port]
193
        mov     [eax + com_mouse_data.port], bx
194
        mov     [eax + com_mouse_data.offset], 0
195
 
196
; Attach int handler
197
 
198
        invoke  AttachIntHandler, [irq], irq_handler, eax
199
        test    eax, eax
200
        jz      .fail
201
 
202
        DEBUGF  1, "Attached int handler\n"
203
 
204
; Enable interrupts
205
        mov     dx, word[port]
206
        inc     dx
207
        mov     al, 1
208
        out     dx, al
209
 
210
        xor     eax, eax
2288 clevermous 211
        ret
212
 
5072 hidnplayr 213
  .free:
214
        DEBUGF  1, "Freeing port area\n"
215
        xor     ebx, ebx
216
        inc     ebx             ; free port area
217
        mov     ecx, [port]
218
        lea     edx, [ecx + 7]
219
        push    ebp
220
        invoke  ReservePortArea
221
        pop     ebp
2288 clevermous 222
 
5072 hidnplayr 223
  .fail:
224
        DEBUGF  1, "Failed\n"
225
        or      eax, -1
226
        ret
2288 clevermous 227
 
5072 hidnplayr 228
endp
229
 
230
 
231
 
2288 clevermous 232
irq_handler:
233
 
5072 hidnplayr 234
        push    esi
235
        mov     esi, [esp+2*4]
236
 
237
  .read_loop:
238
        mov     dx, [esi + com_mouse_data.port]
239
        add     dx, 5
2288 clevermous 240
        in      al, dx
5072 hidnplayr 241
        test    al, 1           ; data ready?
242
        jz      .end
243
; read data
244
        sub     dx, 5
2288 clevermous 245
        in      al, dx
5072 hidnplayr 246
        and     al, 01111111b   ; clear MSB (use 7 bit words)
2288 clevermous 247
 
5072 hidnplayr 248
; Check which data byte we are reading
249
        cmp     [esi + com_mouse_data.offset], 2
250
        ja      .reset
251
        je      .ThirdByte
2288 clevermous 252
        jp      .SecondByte
5072 hidnplayr 253
 
254
; read first data byte
255
        test    al, 01000000b   ; First byte indicator set?
256
        jz      .reset
257
        mov     [esi + com_mouse_data.data+0], al
258
        inc     [esi + com_mouse_data.offset]
259
        jmp     .read_loop
260
 
261
; read second data byte
262
  .SecondByte:
263
        test    al, 01000000b   ; First byte indicator set?
264
        jnz     .reset
265
        mov     [esi + com_mouse_data.data+1], al
266
        inc     [esi + com_mouse_data.offset]
267
        jmp     .read_loop
268
 
269
; read third data byte
270
  .ThirdByte:
271
        test    al, 01000000b   ; First byte indicator set?
272
        jnz     .reset
273
        mov     [esi + com_mouse_data.data+2], al
274
 
275
; Data packet is complete, parse it and set mouse data
276
 
277
; Buttons
278
        mov     al, [esi + com_mouse_data.data+0]
2288 clevermous 279
        mov     ah, al
5072 hidnplayr 280
        shr     al, 3           ; right mouse button
281
        and     al, 2           ;
282
        shr     ah, 5           ; left mouse button
283
        and     ah, 1           ;
2288 clevermous 284
        add     al, ah
285
        movzx   eax, al
286
        mov     [BTN_DOWN], eax
287
 
5072 hidnplayr 288
; X coordinate
289
        mov     al, [esi + com_mouse_data.data+0]
2288 clevermous 290
        shl     al, 6
5072 hidnplayr 291
        or      al, [esi + com_mouse_data.data+1]
292
        movsx   eax, al
2288 clevermous 293
        mov     [MOUSE_X], eax
294
 
5072 hidnplayr 295
; Y coordinate
296
        mov     al, [esi + com_mouse_data.data+0]
2288 clevermous 297
        and     al, 00001100b
298
        shl     al, 4
5072 hidnplayr 299
        or      al, [esi + com_mouse_data.data+2]
300
        movsx   eax, al
2288 clevermous 301
        neg     eax
302
        mov     [MOUSE_Y], eax
303
 
5072 hidnplayr 304
        invoke  SetMouseData, [BTN_DOWN], [MOUSE_X], [MOUSE_Y], 0, 0
2288 clevermous 305
 
5072 hidnplayr 306
  .reset:
307
        mov     [esi + com_mouse_data.offset], 0
308
  .end:
309
        pop     esi
2288 clevermous 310
        mov     al, 1
311
        ret
312
 
313
 
5072 hidnplayr 314
; End of code
2288 clevermous 315
 
5072 hidnplayr 316
data fixups
317
end data
2288 clevermous 318
 
5072 hidnplayr 319
include '../peimport.inc'
2288 clevermous 320
 
5072 hidnplayr 321
my_service   db 'commouse',0    ; max 16 chars include zero
2288 clevermous 322
 
5072 hidnplayr 323
include_debug_strings
2288 clevermous 324
 
5072 hidnplayr 325
MOUSE_X      dd ?
326
MOUSE_Y      dd ?
327
BTN_DOWN     dd ?