Subversion Repositories Kolibri OS

Rev

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

  1. format MS COFF
  2.  
  3. DEBUG   equ 1
  4.  
  5. include '../../../proc32.inc'
  6. include '../../../imports.inc'
  7.  
  8. struc IOCTL
  9. {  .handle      dd ?
  10.    .io_code     dd ?
  11.    .input       dd ?
  12.    .inp_size    dd ?
  13.    .output      dd ?
  14.    .out_size    dd ?
  15. }
  16.  
  17. virtual at 0
  18.   IOCTL IOCTL
  19. end virtual
  20.  
  21. public START
  22. public version
  23.  
  24. DRV_ENTRY  equ 1
  25. DRV_EXIT   equ -1
  26.  
  27. MT_3B       equ 0
  28. MT_3BScroll equ 3
  29. MT_5BScroll equ 4
  30.  
  31. PS2_DRV_VER equ 1
  32.  
  33. section '.flat' code readable align 16
  34.  
  35.  
  36. proc START stdcall, state:dword
  37.  
  38.           cmp [state], DRV_ENTRY
  39.           jne .nothing
  40.   .init:
  41. ; disable keyboard and mouse interrupts
  42. ; keyboard IRQ handler can interfere badly otherwise
  43.           pushf
  44.           cli
  45.           mov  bl, 0x20        ; read command byte
  46.           call kbd_cmd
  47.           test ah,ah
  48.           jnz  .fin
  49.           call kbd_read
  50.           test ah,ah
  51.           jnz  .fin
  52.           popf
  53.           and  al, 0xFC        ; disable interrupts
  54.           or   al, 0x10        ; disable keyboard
  55.           push eax
  56.           mov  bl, 0x60        ; write command byte
  57.           call kbd_cmd
  58.           pop  eax
  59.           call kbd_write
  60.  
  61.           call detect_mouse
  62.           test eax,eax
  63.           jnz  .exit
  64.  
  65.           mov  [MouseType],MT_3B
  66.  
  67.           call try_mode_ID3
  68.           test eax,eax
  69.           jnz  .stop_try
  70.           mov  [MouseType],MT_3BScroll
  71.          
  72.           call try_mode_ID4
  73.           test eax,eax
  74.           jnz  .stop_try
  75.           mov  [MouseType],MT_5BScroll
  76.          
  77.   .stop_try:
  78.  
  79.           mov  al, 0xF4        ; enable data reporting
  80.           call mouse_cmd
  81.  
  82. ; enable keyboard and mouse interrupts
  83.           mov  bl, 0x20        ; read command byte
  84.           call kbd_cmd
  85.           call kbd_read
  86.           or   al, 3           ; enable interrupts
  87.           and  al, not 0x10    ; enable keyboard
  88.           push eax
  89.           mov  bl, 0x60        ; write command byte
  90.           call kbd_cmd
  91.           pop  eax
  92.           call kbd_write
  93.  
  94.           stdcall AttachIntHandler, 12, irq_handler, 0
  95.           stdcall RegService, my_service, service_proc
  96.                 ret
  97.  
  98.   .fin:
  99.           popf
  100.           ;stdcall DetachIntHandler, 12, irq_handler
  101.           mov  bl, 0xA7        ; disable mouse interface
  102.           call kbd_cmd
  103.   .nothing:
  104.           xor  eax, eax
  105.           ret
  106.  
  107.   .exit:
  108.           mov  bl, 0xA7        ; disable mouse interface
  109.           call kbd_cmd
  110.  
  111. ; enable keyboard interrupt, leave mouse interrupt disabled
  112.           mov  bl, 0x20        ; read command byte
  113.           call kbd_cmd
  114.           call kbd_read
  115.           or   al, 1           ; enable keyboard interrupt
  116.           and  al, not 0x10    ; enable keyboard
  117.           push eax
  118.           mov  bl, 0x60        ; write command byte
  119.           call kbd_cmd
  120.           pop  eax
  121.           call kbd_write
  122.  
  123.           xor  eax, eax
  124.           ret
  125. endp
  126.  
  127. proc service_proc stdcall, ioctl:dword
  128.     mov  edi, [ioctl]
  129.     mov  eax, [edi+IOCTL.io_code]
  130.     test eax, eax
  131.     jz   .getversion
  132.     cmp  eax,1
  133.     jz   .gettype
  134.  
  135.   .err:
  136.     or   eax, -1
  137.     ret
  138.  
  139.   .ok:
  140.     xor  eax, eax
  141.     ret
  142.  
  143.   .getversion:
  144.     cmp  [edi+IOCTL.out_size], 4
  145.     jb   .err
  146.     mov  edi, [edi+IOCTL.output]
  147.     mov  dword [edi], PS2_DRV_VER               ; version of driver
  148.     jmp  .ok
  149.   .gettype:
  150.     cmp  [edi+IOCTL.out_size], 4
  151.     jb   .err
  152.     mov  edi, [edi+IOCTL.output]
  153.     mov  eax,[MouseType]
  154.     mov  dword [edi], eax               ; mouse type
  155.     jmp  .ok
  156. endp
  157.  
  158. detect_mouse:
  159.  
  160.     mov  bl, 0xA8            ; enable mouse interface
  161.     call kbd_cmd
  162.     cmp  ah,1
  163.     je   .fail
  164.  
  165.           mov  al, 0xFF      ; reset
  166.     call mouse_cmd
  167.     jc   .fail
  168.  
  169.     call mouse_read
  170.     jc   .fail
  171.     cmp  al, 0xAA
  172.     jne  .fail         ; dead mouse
  173.  
  174.     ; get device ID
  175.     call mouse_read
  176.     jc   .fail
  177.     cmp  al, 0x00
  178.     jne  .fail        ; unknown device
  179.  
  180.     xor  eax,eax
  181.     ret
  182.  
  183.   .fail:
  184.     or   eax,-1
  185.     ret
  186.  
  187. try_mode_ID3:
  188.     mov  al, 0xF3    ;Set Sample Rate
  189.     call mouse_cmd
  190.     jc   .fail
  191.     mov  al, 0xC8    ;200d
  192.     call mouse_cmd
  193.     jc   .fail
  194.     mov  al, 0xF3    ;Set Sample Rate
  195.     call mouse_cmd
  196.     jc   .fail
  197.     mov  al, 0x64    ;100d
  198.     call mouse_cmd
  199.     jc   .fail
  200.     mov  al, 0xF3    ;Set Sample Rate
  201.     call mouse_cmd
  202.     jc   .fail
  203.     mov  al, 0x50    ;80d
  204.     call mouse_cmd
  205.     jc   .fail
  206.  
  207.     mov  al, 0xF2    ;Get device id
  208.     call mouse_cmd
  209.     jc   .fail
  210.  
  211.     call mouse_read
  212.     jc   .fail
  213.     cmp  al, 0x03
  214.     jne  .fail
  215.  
  216.     xor  eax,eax
  217.     ret
  218.   .fail:
  219.     or   eax,-1
  220.     ret
  221.  
  222. try_mode_ID4:
  223.     mov  al, 0xF3    ;Set Sample Rate
  224.     call mouse_cmd
  225.     jc   .fail
  226.     mov  al, 0xC8    ;200d
  227.     call mouse_cmd
  228.     jc   .fail
  229.     mov  al, 0xF3    ;Set Sample Rate
  230.     call mouse_cmd
  231.     jc   .fail
  232.     mov  al, 0xC8    ;100d
  233.     call mouse_cmd
  234.     jc   .fail
  235.     mov  al, 0xF3    ;Set Sample Rate
  236.     call mouse_cmd
  237.     jc   .fail
  238.     mov  al, 0x50    ;80d
  239.     call mouse_cmd
  240.     jc   .fail
  241.  
  242.     mov  al, 0xF2    ;Get device id
  243.     call mouse_cmd
  244.     jc   .fail
  245.  
  246.     call mouse_read
  247.     jc   .fail
  248.     cmp  al, 0x04
  249.     jne  .fail
  250.  
  251.     xor  eax,eax
  252.     ret
  253.  
  254.   .fail:
  255.     or   eax,-1
  256.     ret
  257.    
  258. include 'ps2m_iofuncs.inc'
  259. include 'ps2m_irqh.inc'
  260.  
  261. section '.data' data readable writable align 16
  262.  
  263. version           dd  0x00050005
  264. my_service      db  'ps2mouse',0
  265.  
  266. ;iofuncs data
  267. mouse_cmd_byte   db 0
  268. mouse_nr_tries   db 0
  269. mouse_nr_resends db 0
  270.  
  271. ;hid data
  272. mouse_byte  dd 0
  273.  
  274. first_byte  db 0
  275. second_byte db 0
  276. third_byte  db 0
  277. fourth_byte db 0
  278.  
  279. ;main data
  280. MouseType        dd 0
  281.  
  282. XMoving          dd 0
  283. YMoving          dd 0
  284. ZMoving          dd 0
  285. ButtonState      dd 0
  286. ;timerTicks       dd 0
  287.