Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2014. All rights reserved. ;;
  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 <Lrz>                                             ;;
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10.  
  11. format PE DLL native
  12. entry START
  13.  
  14.         CURRENT_API             = 0x0200
  15.         COMPATIBLE_API          = 0x0100
  16.         API_VERSION             = (COMPATIBLE_API shl 16) + CURRENT_API
  17.  
  18.         __DEBUG__               = 1
  19.         __DEBUG_LEVEL__         = 2
  20.  
  21. section '.flat' readable writable executable
  22.  
  23. include '../proc32.inc'
  24. include '../struct.inc'
  25. include '../macros.inc'
  26. include '../fdo.inc'
  27.  
  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
  33.  
  34. struct com_mouse_data
  35.  
  36.         port            dw ?
  37.         offset          db ?
  38.         data            rb 3
  39.  
  40. ends
  41.  
  42. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  43. ;;                        ;;
  44. ;; proc START             ;;
  45. ;;                        ;;
  46. ;; (standard driver proc) ;;
  47. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  48.  
  49. proc START c, reason:dword, cmdline:dword
  50.  
  51.         cmp     [reason], DRV_ENTRY
  52.         jne     .fail
  53.  
  54.         DEBUGF  2,"Loading serial mouse driver\n"
  55.  
  56.         stdcall init_mouse, 0x3f8, 4
  57.         stdcall init_mouse, 0x2f8, 3
  58.         stdcall init_mouse, 0x3e8, 4
  59.         stdcall init_mouse, 0x2e8, 3
  60.  
  61.         invoke  RegService, my_service, service_proc
  62.         ret
  63.  
  64.   .fail:
  65.         xor     eax, eax
  66.         ret
  67.  
  68. endp
  69.  
  70.  
  71. proc service_proc stdcall, ioctl:dword
  72.  
  73.         mov     ebx, [ioctl]
  74.         mov     eax, [ebx + IOCTL.io_code]
  75.         cmp     eax, 0 ;SRV_GETVERSION
  76.         jne     .fail
  77.  
  78.         mov     eax, [ebx + IOCTL.output]
  79.         cmp     [ebx + IOCTL.out_size], 4
  80.         jne     .fail
  81.         mov     [eax], dword API_VERSION
  82.         xor     eax, eax
  83.         ret
  84.  
  85.   .fail:
  86.         or      eax, -1
  87.         ret
  88. endp
  89.  
  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
  109.         mov     dx, bx
  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
  118.         inc     dx
  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
  147.         mov     al, 1
  148.         out     dx, al
  149.  
  150. ; Wait 5 ticks (0.2s)
  151.         mov     esi, 200
  152.         invoke  Sleep
  153.  
  154. ; Clear data register
  155.         mov     dx, bx
  156.         in      al, dx
  157.  
  158. ; set DTR, DTS and OUT2
  159.         add     dx, 4
  160.         mov     al, 1011b
  161.         out     dx, al
  162.  
  163.         mov     ecx, 0x1FFFF
  164. ; Poll port
  165.   .loop:
  166.         dec     ecx
  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
  211.         ret
  212.  
  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
  222.  
  223.   .fail:
  224.         DEBUGF  1, "Failed\n"
  225.         or      eax, -1
  226.         ret
  227.  
  228. endp
  229.  
  230.  
  231.  
  232. irq_handler:
  233.  
  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
  240.         in      al, dx
  241.         test    al, 1           ; data ready?
  242.         jz      .end
  243. ; read data
  244.         sub     dx, 5
  245.         in      al, dx
  246.         and     al, 01111111b   ; clear MSB (use 7 bit words)
  247.  
  248. ; Check which data byte we are reading
  249.         cmp     [esi + com_mouse_data.offset], 2
  250.         ja      .reset
  251.         je      .ThirdByte
  252.         jp      .SecondByte
  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]
  279.         mov     ah, al
  280.         shr     al, 3           ; right mouse button
  281.         and     al, 2           ;
  282.         shr     ah, 5           ; left mouse button
  283.         and     ah, 1           ;
  284.         add     al, ah
  285.         movzx   eax, al
  286.         mov     [BTN_DOWN], eax
  287.  
  288. ; X coordinate
  289.         mov     al, [esi + com_mouse_data.data+0]
  290.         shl     al, 6
  291.         or      al, [esi + com_mouse_data.data+1]
  292.         movsx   eax, al
  293.         mov     [MOUSE_X], eax
  294.  
  295. ; Y coordinate
  296.         mov     al, [esi + com_mouse_data.data+0]
  297.         and     al, 00001100b
  298.         shl     al, 4
  299.         or      al, [esi + com_mouse_data.data+2]
  300.         movsx   eax, al
  301.         neg     eax
  302.         mov     [MOUSE_Y], eax
  303.  
  304.         invoke  SetMouseData, [BTN_DOWN], [MOUSE_X], [MOUSE_Y], 0, 0
  305.  
  306.   .reset:
  307.         mov     [esi + com_mouse_data.offset], 0
  308.   .end:
  309.         pop     esi
  310.         mov     al, 1
  311.         ret
  312.  
  313.  
  314. ; End of code
  315.  
  316. data fixups
  317. end data
  318.  
  319. include '../peimport.inc'
  320.  
  321. my_service   db 'commouse',0    ; max 16 chars include zero
  322.  
  323. include_debug_strings
  324.  
  325. MOUSE_X      dd ?
  326. MOUSE_Y      dd ?
  327. BTN_DOWN     dd ?