;       SCCSID = @(#)bkshwid.asm	6.1 90/11/16
;	SCCSID = @(#)bkshwid.asm	6.8 90/03/27
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
	PAGE	60,132
	TITLE	BKSHWID.ASM - Get Hardware ID

;******************************************************************************
;*******   C H A N G E	     H I S T O R Y   **********************************
;******************************************************************************
;@T52	90/01/15  TPL	DCR 704 work
;B712146 THG 03-27-90  Ensure that DOSDEVIOCTL is passed a valid KBD$ handle
;B712146	       from a TextWindow session caller

	.286p				; 286 Protect Mode instructions

	.xlist
	INCLUDE error2.inc		; Extended error code file
	INCLUDE struc.inc		; Structured Macros
	INCLUDE bksxxx.inc		; Equates and structure template file
	INCLUDE bksextrn.inc		; External data declaration file
	.list

CSEG	SEGMENT BYTE PUBLIC 'CODE'
CSEG	ENDS

IDSEG	SEGMENT BYTE PUBLIC 'DATA'      ; Per process instance data segment
IDSEG	ENDS
;
;	System services routine name(s)
;
	EXTRN	DOSDEVIOCTL:FAR 	; Get in use codepage ID
	EXTRN	DOSOPEN:FAR		; Get KBD$ handle	       ;B712146
	EXTRN	DOSCLOSE:FAR		; Close handle		       ;B712146

	GDSEG	SEGMENT BYTE PUBLIC 'FAR_DATA'                          ;@T52
	GDSEG	ENDS

	PUBLIC	GetHWID

CSEG	SEGMENT BYTE PUBLIC 'CODE'
	ASSUME	CS:CSEG,DS:NOTHING,ES:NOTHING
	PAGE

;********************START OF SPECIFICATIONS****************************
;
; ROUTINE NAME: GetHWID
;
; FUNCTION:	Return the keyboard hardware ID.
;
; LINKAGE:	push %HWID% selector
;		push %HWID% offset
;		push %HANDLE%
;		call KBDGETHWID:FAR
;		push function-code
;		call KbdRout:near
;		push DS
;		call BKSMAIN:FAR
;		call GetHWID:near
;
; PARAMETERS :	%HWID%	  DWORD   ptr to data structure
;
;			HWID struc
;			  LENGTH  dw  4  ;length of structure (input/output)
;			  ID	  dw  ?  ;keyboard ID (output)
;			HWID ends
;
;
;		%HANDLE%  WORD (from KBDOPEN)
;
; EXIT-NORMAL:	AX = 0
;
; EXIT-ERROR:	AX = ERROR_KBD_PARAMETER
;
; INTERNAL REFERENCES:	IDSEG - instance data segment
;	   ROUTINES:	none
;
; EXTERNAL REFERENCES:	none
;	   ROUTINES:	DOSDEVIOCTL - Get the keyboard hardware ID
;
;******************END OF SPECIFICATIONS********************************
;
; GetHWID pseudo-code:
;
; if %HWID%.LENGTH < 4
;   AX <-- ERROR_KBD_PARAMETER
;
;   if %HWID%.LENGTH > 1
;     %HWID%.LENGTH <-- 4
;   endif
;
; endif
;
; if AX == 0
;   call DOSDEVIOCTL to get keyboard hardware ID
; endif
;
; return




GetHWID proc near
   ASSUME  DS:IDSEG

   ;************************************************************************
   ;*  On entry:  DS = IDSEG   AX = 0	      BP+14 = first parameter	   *
   ;*		  ES = GDSEG   DX = KBD_SG				   *
   ;************************************************************************

   push bx	;save BX, BX will hold KBD$ handle if necessary
   push es
   push di

   les	di,[bp+16]

   .if <<word ptr es:[di]> b 4>       ;length must be >= 4 for success
     mov  ax,ERROR_KBD_PARAMETER

     .if <<word ptr es:[di]> a 0>     ;if room for return length
       mov  byte ptr es:[di],4	      ;set length required to 4
     .endif			      ;

   .endif

   pop	di
   pop	es

   .if <zero ax>

     .if <dx A LastBaseSG>	;Text-Window session?		       ;B712146
       push 0			;SS:AX <-- local word for handle       ;B712146
       mov  ax,sp		;				       ;B712146

       push es			; Setup:  Address of device name       ;B712146
       push OFFSET Dev_Name	;				       ;B712146
       push ss			;	  Address of device handle     ;B712146
       push ax			;	   to be returned by DOS       ;B712146
       push es			;	  Address of action taken      ;B712146
       push OFFSET Action_Taken ;	   by DOS		       ;B712146
       push 0			; file size = 0 (dword) 	       ;B712146
       push 0			;				       ;B712146
       push 0			; file attribute		       ;B712146
       push 01H 		; open flag - fail if device	       ;B712146
				;   does not exists		       ;B712146
       push 0C2H		; open mode = ISSSRAAA		       ;B712146
				;	      11000010		       ;B712146
				; I   - Non inheritable handle	       ;B712146
				; SSS - Deny None access	       ;B712146
				; R   - Reserved must be 0	       ;B712146
				; AAA - Read/Write access	       ;B712146
       push 0			; Double words of 0		       ;B712146
       push 0			;				       ;B712146
       call DOSOPEN		;				       ;B712146
       pop  bx			;BX <-- KBD$ handle		       ;B712146

       .if <zero ax>						       ;B712146
	 push  [bp+18]		; Setup:  Address of KbdID	       ;B712146
	 push  [bp+16]		;	    to be returned	       ;B712146
	 push  0		;	  Address of Dummy DATA        ;B712146
	 push  0		;	    parmlist		       ;B712146
	 push  7AH		;	  Func. 7AH = GetHWID	       ;B712146
	 push  KEYBOARD 	;	  Category 4 = Keyboard        ;B712146
	 push  bx		;	  Device handle 	       ;B712146
	 call  DOSDEVIOCTL	; Get keyboard hardware ID	       ;B712146
       .endif							       ;B712146

       push ax			;close handle opened for IOCtl	       ;B712146
       push bx			;				       ;B712146
       call DOSCLOSE		;				       ;B712146
       pop  ax			;				       ;B712146
     .else							       ;B712146
       push  [bp+18]		; Setup:  Address of KbdID
       push  [bp+16]		;	    to be returned
       push  0			;	  Address of Dummy DATA
       push  0			;	    parmlist
       push  7AH		;	  Func. 7AH = GetHWID
       push  KEYBOARD		;	  Category 4 = Keyboard
       push  Dev_Handle 	;	  Device handle
       call  DOSDEVIOCTL	; Get keyboard hardware ID
     .endif

   .endif

   pop	bx		;BX was used to hold KBD$ handle	       ;B712146
   ret
GetHWID endp

CSEG ENDS
     END
