home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / asdt32.zip / SAMPLEDD.ASM < prev    next >
Assembly Source File  |  1992-03-28  |  12KB  |  337 lines

  1.         page    ,132
  2.         title   Sample Device Driver for OS/2 2.0
  3.         subttl  Copyright (C) 1992 by the IBM Corporation
  4.  
  5. ; This file contains the code for a sample device driver.  The purpose
  6. ; of the device driver is to illustrate the usage of ASDT32.
  7. ;
  8. ; The original code was written 3/92 by Jim Christensen and modified for
  9. ; ASDT32 purposes 4/92 by Dave Evans.
  10.  
  11.         .386p
  12.         .lall
  13.  
  14. DEBUG_HOOK      macro
  15.                 int 3           ; Debug hook for ASDT32
  16. endm
  17.  
  18. ; The following hard-coded constants are a kludge.  They give the address
  19. ; of the BIOS data area that holds the number of columns on the screen.  Note
  20. ; that this area is of little use to protect-mode OS/2 2.0 screen handling,
  21. ; but I know that it is always accessible.
  22.  
  23. COLUMNS_SEL     equ 0040h       ; 16-bit seglector to BIOS data area
  24. COLUMNS_OFF     equ 004Ah       ; 16-bit offset to screen column count
  25.  
  26.  
  27. ; This device driver must be installed at system boot time as an OS/2 device
  28. ; driver.  To use this device driver, an OS/2 program must do the following:
  29. ;
  30. ; 1.  DosOpen( "$SAMPDD" )
  31. ;
  32. ; 2.  DosDevIOCtl( NULL, &parms, 0xF0, 0xE0, openHandle )
  33. ;
  34. ; 3.  DosClose( openHandle )
  35. ;
  36. ;     Handle is the 16-bit number returned by DosOpen.
  37.  
  38.  
  39. ; Device Header
  40.  
  41. DEVATTR         equ  1000100010000000b
  42. ;                    |   | |||
  43. ;                    |   | Level (DOS 5.0)
  44. ;                    |   Accepts Open/Close/Removable Media
  45. ;                    Device is a character device
  46.  
  47. DevHeader       struc
  48. dh_next         dd ?
  49. dh_attr         dw DEVATTR
  50. dh_strategy     dw ?
  51. dh_1            dw 0
  52. dh_name         db "        "
  53. dh_2            db 8 dup(0)
  54. DevHeader       ends
  55.  
  56.  
  57. ; Request Packet
  58.  
  59. RPMAXLEN        equ 18          ; Maximum size of packet data
  60.  
  61. ReqPacket       struc
  62. rp_len          db ?            ; Length in bytes of packet
  63. rp_unit         db ?            ; Subunit number of block device
  64. rp_cmd          db ?            ; Command code
  65. rp_status       dw ?            ; Status word
  66. rp_doslink      dd ?            ; Reserved
  67. rp_devlink      dd ?            ; Device multiple-request link
  68. rp_data         db RPMAXLEN dup (?)
  69.                                 ; Data pertaining to specific packet
  70. ReqPacket       ends
  71.  
  72. ; Values for the rp_status field
  73.  
  74. PSTAT_ERROR     equ   8000h     ; Error status
  75. PSTST_BUSY      equ   0200h     ; Busy status
  76. PSTAT_DONE      equ   0100h     ; Command complete
  77. PSTAT_UNKCMD    equ   0003h     ; Unknown command
  78. PSTAT_GFAIL     equ   000Ch     ; General failure
  79. PSTAT_BADPARM   equ   0013h     ; Invalid parameter
  80.  
  81. ; Subfields of the rp_data field
  82.  
  83. InitDevHlp      equ dword ptr rp_data+1 ; Pointer to DevHlp commands
  84. InitEndCode     equ  word ptr rp_data+1 ; Code segment size
  85. InitEndData     equ  word ptr rp_data+3 ; Data segment size
  86. IOaddr          equ dword ptr rp_data+1 ; Transfer address (32 bits)
  87. IOaddrLow       equ  word ptr rp_data+1 ; Transfer address (low 16 bits)
  88. IOaddrHigh      equ  word ptr rp_data+3 ; Transfer address (high 16 bits)
  89. IOcount         equ  word ptr rp_data+5 ; Count of bytes/sectors
  90.  
  91. IoctlFcat       equ  byte ptr rp_data+0 ; Function category
  92. IoctlFcode      equ  byte ptr rp_data+1 ; Function code
  93. IoctlParmAddr   equ dword ptr rp_data+2 ; Parameter buffer address
  94. IoctlDataAddr   equ dword ptr rp_data+6 ; Data buffer address
  95.  
  96. ; The values expected by this device driver for an ioctl call are:
  97. IOCTL_CATEGORY         equ 0E0h
  98. IOCTL_FUNCTION_INIT    equ 0F0h
  99.  
  100. DDioctl_Request  struc
  101. DDioctl_ptr      dd ?   ; Returned address of BIOS screen column count
  102. DDioctl_Request  ends
  103.  
  104.  
  105. ; Command Code for DevHelp Functions
  106.  
  107. DevHlp_SemRequest       equ  6  ; Claim a semaphore
  108. DevHlp_SemClear         equ  7  ; Release a semaphore
  109. DevHlp_QueueInit        equ 15  ; Init/Clear char queue
  110. DevHlp_QueueFlush       equ 16  ; Flush queue
  111. DevHlp_QueueWrite       equ 17  ; Put a char in the queue
  112. DevHlp_QueueRead        equ 18  ; Get a char from the queue
  113. DevHlp_Lock             equ 19  ; Lock segment into storage
  114. DevHlp_Unlock           equ 20  ; Unlock segment in storage
  115. DevHlp_PhysToVirt       equ 21  ; Convert physical address to virtual
  116. DevHlp_VirtToPhys       equ 22  ; Convert virtual address to physical
  117. DevHlp_PhysToUVirt      equ 23  ; Convert physical to user virtual
  118. DevHlp_AllocPhys        equ 24  ; Allocate physical memory
  119. DevHlp_SetIRQ           equ 27  ; Set a hardware interrupt vector
  120. DevHlp_SetTimer         equ 29  ; Set timer request handler
  121. DevHlp_MonitorCreate    equ 31  ; Create a monitor
  122. DevHlp_Register         equ 32  ; Install a monitor
  123. DevHlp_DeRegister       equ 33  ; Remove a monitor
  124. DevHlp_MonWrite         equ 34  ; Pass data records to monitor
  125. DevHlp_MonFlush         equ 35  ; Remove all data from stream
  126. DevHlp_GetDOSVar        equ 36  ; Return pointer to DOS variable
  127. DevHlp_VerifyAccess     equ 39  ; Verify access to virtual memory
  128. DevHlp_AllocGDTSelector equ 45  ; Allocate GDT Descriptors
  129. DevHlp_EOI              equ 49  ; Issue an End-of-Interrupt
  130. DevHlp_UnPhysToVirt     equ 50  ; Done with PhysToVirt address
  131.  
  132. ;------------------------------------------------------------------------------
  133. ; Data Segment
  134. ;------------------------------------------------------------------------------
  135.  
  136. data    segment use16 public 'DATA'
  137.  
  138.         DevHeader <-1,, code:Strategy,, "$SAMPDD">
  139.  
  140.  
  141. router_table    label word
  142.         dw      Init            ; Command code 0: init
  143.         dw      Unsupported     ; Command code 1: media check
  144.         dw      Unsupported     ; Command code 2: build BPB
  145.         dw      Unsupported     ; Command code 3: reserved
  146.         dw      Unsupported     ; Command code 4: read (input)
  147.         dw      Unsupported     ; Command code 5: nondestructive read no wait
  148.         dw      Unsupported     ; Command code 6: input status
  149.         dw      Unsupported     ; Command code 7: input flush
  150.         dw      Unsupported     ; Command code 8: write (output)
  151.         dw      Unsupported     ; Command code 9: write with verify
  152.         dw      Unsupported     ; Command code 10: output status
  153.         dw      Unsupported     ; Command code 11: output flush
  154.         dw      Unsupported     ; Command code 12: reserved
  155.         dw      Open            ; Command code 13: device open
  156.         dw      Close           ; Command code 14: device close
  157.         dw      Unsupported     ; Command code 15: removable media
  158.         dw      Control         ; Command code 16: generic ioctl
  159.         dw      Unsupported     ; Command code 17: reset media
  160.         dw      Unsupported     ; Command code 18: get logical drive map
  161.         dw      Unsupported     ; Command code 19: set logical drive map
  162.         dw      Unsupported     ; Command code 20: deinstall
  163.         dw      Unsupported     ; Command code 21: reserved
  164.         dw      Unsupported     ; Command code 22: partitionable fixed disks
  165.         dw      Unsupported     ; Command code 23: get fixed disk/log.unit map
  166.         dw      Unsupported     ; Command code 24: reserved
  167.         dw      Unsupported     ; Command code 25: reserved
  168.         dw      Unsupported     ; Command code 26: reserved
  169.  
  170. RTABLE_SIZE     equ ($ - router_table) / 2
  171.  
  172. ;------------------------------------------------------------------------------
  173. ; Variables
  174. ;------------------------------------------------------------------------------
  175.  
  176.         public  DevHlp
  177. DevHlp          dd 0            ; Entry point for DevHlp routines
  178.         public  rp
  179. rp              dd 0            ; Request packet address
  180.  
  181.         public  physDS
  182. physDS          dd 0
  183.  
  184. EndOfData       equ $           ; Rest of data released after initialization
  185.  
  186.  
  187.         public  InitMsg
  188. InitMsg         equ $
  189.  db 13,10
  190.  db "SAMPLEDD.SYS installed (v.2.6.307B) -- IBM Internal Use Only",13,10
  191.  db "     Copyright (C) 1992 by the IBM Corporation",13,10
  192. InitMsgLen      equ $-InitMsg
  193.  
  194. InitWord        dw 0
  195.  
  196. data    ends
  197. ;------------------------------------------------------------------------------
  198.  
  199.         extrn   DosWrite:far
  200. code    segment use16 public 'CODE'
  201.         assume  cs:code, ds:data
  202.  
  203.         public  Strategy
  204. Strategy        proc far
  205.         DEBUG_HOOK              ; Conditionally allow ASDT32 control
  206.         mov     word ptr ds:rp+0, bx
  207.         mov     word ptr ds:rp+2, es
  208.         mov     al, es:[bx].rp_cmd
  209.         cmp     al, RTABLE_SIZE
  210.         ja      BadCmd
  211.  
  212.         xor     ah, ah
  213.         shl     ax, 1
  214.         xchg    ax, di
  215.         xor     ax, ax
  216.         call    router_table [di]
  217.         les     bx, ds:rp
  218.         or      ax, PSTAT_DONE
  219.         mov     es:[bx].rp_status, ax
  220.         ret
  221. BadCmd:
  222.         mov     es:[bx].rp_status, PSTAT_DONE + PSTAT_ERROR + PSTAT_UNKCMD
  223.         ret
  224. Strategy        endp
  225.  
  226. Unsupported     proc
  227.         mov     ax, PSTAT_ERROR + PSTAT_UNKCMD
  228.         ret
  229. Unsupported     endp
  230.  
  231. CantDoThat      proc
  232.         mov     ax, PSTAT_ERROR + PSTAT_GFAIL
  233.         ret
  234. CantDoThat      endp
  235.  
  236. Open            proc
  237.         xor     ax, ax
  238.         ret
  239. Open            endp
  240.  
  241. Close           proc
  242.         xor     ax, ax
  243.         ret
  244. Close           endp
  245.  
  246. ;------------------------------------------------------------------------------
  247. ; This routine processes the IOCTL request
  248. ;------------------------------------------------------------------------------
  249.  
  250. BadArg          proc
  251.         mov     ax, PSTAT_ERROR + PSTAT_BADPARM
  252.         ret
  253. BadArg          endp
  254.  
  255.         public  Control
  256. Control         proc
  257.         cmp     es:[bx].IoctlFcat, IOCTL_CATEGORY
  258.         jne     BadArg
  259.         cmp     es:[bx].IoctlFcode, IOCTL_FUNCTION_INIT
  260.         jne     BadArg
  261.  
  262. ; Make sure the caller has read/write access to the IOCTL parameter area.
  263.  
  264.         les     bx, ds:rp
  265.         mov     di, word ptr es:[bx].IoctlParmAddr + 0
  266.         mov     ax, word ptr es:[bx].IoctlParmAddr + 2
  267.         mov     cx, size DDioctl_Request
  268.         mov     dh, 01          ; Read/write access
  269.         mov     dl, DevHlp_VerifyAccess
  270.         call    [DevHlp]
  271.         jc      BadArg
  272.  
  273. ; Map the interrupt count into a user segment
  274.  
  275.         push    ds
  276.         push    ds
  277.         pop     es
  278.         mov     ax, COLUMNS_SEL
  279.         mov     ds, ax
  280.         mov     si, COLUMNS_OFF
  281.         mov     dl, DevHlp_VirtToPhys
  282.         call    es:[DevHlp]
  283.         pop     ds
  284.         jc      CantDoThat
  285.         mov     word ptr physDS+0, bx
  286.         mov     word ptr physDS+2, ax
  287.  
  288.         les     bx, ds:rp
  289.         les     bp, es:[bx].IoctlParmAddr   ;es:bp -> IOCTL parm area
  290.         push    es
  291.         mov     bx, word ptr physDS+0
  292.         mov     ax, word ptr physDS+2
  293.         mov     cx, 2
  294.         mov     dh, 0           ; Read-only access
  295.         mov     dl, DevHlp_PhysToUVirt
  296.         call    [DevHlp]
  297.         mov     ax, es          ; ax:bx = virt addr
  298.         pop     es              ; es:bp -> IOCTL parm area
  299.         jc      CantDoThat
  300.         mov     word ptr es:[bp].DDioctl_ptr+0, bx
  301.         mov     word ptr es:[bp].DDioctl_ptr+2, ax
  302.  
  303.         xor     ax, ax
  304.         ret
  305. Control         endp
  306.  
  307. EndOfCode       equ $           ; Rest of code released after initialization
  308.  
  309. ;------------------------------------------------------------------------------
  310. ; This initialization code is called just once during system boot.
  311. ;------------------------------------------------------------------------------
  312.         public  Init
  313. Init            proc
  314.         mov     ax, word ptr es:[bx].InitDevHlp + 0
  315.         mov     word ptr DevHlp + 0, ax
  316.         mov     ax, word ptr es:[bx].InitDevHlp + 2
  317.         mov     word ptr DevHlp + 2, ax
  318.  
  319.         push    1               ; Standard output device
  320.         push    ds
  321.         push    offset InitMsg
  322.         push    InitMsgLen
  323.         push    ds
  324.         push    offset InitWord
  325.         call    DosWrite
  326.  
  327.         les     bx, ds:rp
  328.         mov     es:[bx].InitEndCode, offset code:EndOfCode
  329.         mov     es:[bx].InitEndData, offset data:EndOfData
  330.  
  331.         xor     ax, ax
  332.         ret
  333. Init            endp
  334.  
  335. code    ends
  336.         end
  337.