⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbmass.asm

📁 dos下的USB源码(包括UHCI
💻 ASM
📖 第 1 页 / 共 5 页
字号:
	TITLE	USBMASS.ASM -- USB Mass Storage Device Functions

;***************************************************************************;
;***************************************************************************;
;**                                                                       **;
;**           (C)Copyright 1985-2003, American Megatrends, Inc.           **;
;**                                                                       **;
;**                          All Rights Reserved.                         **;
;**                                                                       **;
;**                6145-F Northbelt Pkwy, Norcross, GA 30071              **;
;**                                                                       **;
;**                          Phone (770)-246-8600                         **;
;**                                                                       **;
;***************************************************************************;
;***************************************************************************;
;***************************************************************************;
; $Header: /BIOS/Corebin/800/Modules/USB2/Template/Core/USBMASS.ASM 11    3/17/03 5:53p Sivagarn $
;
; $Revision: 11 $
;
; $Date: 3/17/03 5:53p $
;***************************************************************************;
; Revision History
; ----------------
; $Log: /BIOS/Corebin/800/Modules/USB2/Template/Core/USBMASS.ASM $
; 
; 11    3/17/03 5:53p Sivagarn
;   - Byte flag 'bUSBInitFlag' is changed to double word 'dUSBInitFlag'
;   - Code cleanup
;   - For hotplug mass storage device support certain routines are split
; into multiple routines (Hotplug support is not yet added)
; 
; 10    1/24/03 7:06p Sivagarn
; BugFix
;  - Bug in returning correct max LBA was fixed
;  - BIOS hang when 4-in-1 flash reader is connected is fixed (Invalid
; read format 
; capacity command was identified as valid command)
; 
; 9     1/09/03 6:31p Sivagarn
; - Code to skip read format capacity command, when the incompatibility
; flag for this command is set, is added
; 
; 8     1/06/03 5:32p Sivagarn
; - Code to set emulation mode to HDD, if a drive with only one partition
; table (of size 530MB or greater) is connected, is added
; 
; 7     1/03/03 7:06p Sivagarn
; - 'bLastBulkCommandStatus' variable name changed to
; 'bLastCommandStatus' as it stores status of control transfer also
; - Get max LUN control transfer command is checked for stall condition.
; If the command is stalled then appropriate corrective action is taken
; as per USB specification 
; - The complex 'USBMassUpdatedeviceGeometry' routine is split into three
; routines for proper code walkthrough.  In the above process, bug in
; returning wrong max LBA for drives more than 8GB is fixed
; 
; 6     12/20/02 6:05p Sivagarn
; Fixed the bug in identifying write protected floppy.  Error status was
; changed by the update geometry routine.
; 
; 5     12/17/02 3:46p Sivagarn
; - Changed copyright message year to 2003
; - Support for specifying 32bit linear buffer address in mass
; transaction is added
; - Added support for issuing Read Format Capacity command to USB drives.
; The new command is issued only if the media is not inserted in the
; drive
; - Bug in returning correct emulation type in USBMassGetFormatType
; routine is fixed
; - Devices upto 530MB are assumed without proper boot record is assumed
; as floppy emulated
; - Bug in long format delay is corrected. Verify command will not have
; extra delay
; - LBA number is stored differently to avoid loss of some sectors due to
; rounding
; 
; 4     10/29/02 6:49p Sivagarn
; - Support for slow mass storage devices is added
;   - Control/bulk and interrupt transfer delay variable is updated
; depending on the delay required
;   - New variable is added to take care of transaction related delay
;   - Delay is introduced during StartUnit command (between command and
; status stage) and read/write/verify command
; - Mass storage error conditions are refined
;   - Added a new variable for tracking mass storage errors
;   - Time out and stall conditions are differentiated properly
; 
; 3     10/14/02 9:01p Sivagarn
;  * Code cleanup
;  * Old boot block related code is removed
;  * Incompatibility flags are checked and code is skipped or added
; according to the flag status
; 
; 2     02/09/20 6:36p Tonylo
; USB001
; Bug fix for USB flash device TREK2000 boot failed.
; 
; 1     9/15/02 5:39p Sivagarn
; Initial AMIUSB 2.20 check-in
; 
;***************************************************************************;


;----------------------------------------------------------------------------
; 		Global options are defined here
;----------------------------------------------------------------------------
OPTION	PROC:PRIVATE
;----------------------------------------------------------------------------

;----------------------------------------------------------------------------
;		Macro and equate files are included here
;----------------------------------------------------------------------------
	INCLUDE	equates.equ
	INCLUDE	usbflag.equ
	INCLUDE	usb.equ
	INCLUDE	mbiosequ.equ
;----------------------------------------------------------------------------
	EXTERN	pUSBMassConsumeBuffer:WORD
	EXTERN	bLastCommandStatus:BYTE
	EXTERN	pUSBTempBuffer:WORD
	EXTERN	wTimeOutValue:WORD

USBMisc_FixedDelay		PROTO NEAR SYSCALL
				; AX - wCount
	EXTERN	DeviceInfoTable:NEAR
	EXTERN	DeviceInfoTableEnd:NEAR
	EXTERN	MassDeviceInfoTable:NEAR
	EXTERN	MassDeviceInfoTableEnd:NEAR


check_cmos_data_far		PROTO FAR

	EXTERN	Q_USB_STORAGE_DEVICE_DELAY:ABS
	EXTERN	Q_USB_MASS_DEV1_EMULATION:ABS
	EXTERN	Q_USB_MASS_DEV2_EMULATION:ABS
	EXTERN	Q_USB_MASS_DEV3_EMULATION:ABS
	EXTERN	Q_USB_MASS_DEV4_EMULATION:ABS
	EXTERN	Q_USB_MASS_DEV5_EMULATION:ABS
	EXTERN	Q_USB_MASS_DEV6_EMULATION:ABS
	EXTERN	dUSBInitFlag:DWORD

;----------------------------------------------------------------------------
;		External function definitions are defined here
;----------------------------------------------------------------------------
USBGetProperDeviceInfoStructure	PROTO NEAR SYSCALL
USBCopyDeviceInfoStruc		PROTO NEAR SYSCALL

USBMem_Alloc			PROTO NEAR SYSCALL
				; AL - bNumBlocks
USBMem_AllocOne			PROTO NEAR SYSCALL
USBMem_Free			PROTO NEAR SYSCALL
				; AL - bNumBlocks, BX - wMemBlock
USBMem_FreeOne			PROTO NEAR SYSCALL	; BX - wMemBlock


USBMiscIssueBulkTransfer	PROTO NEAR SYSCALL

USBIssueControlTransfer			PROTO NEAR SYSCALL
USBIssueControlTransferWithoutData	PROTO NEAR SYSCALL
;----------------------------------------------------------------------------

;----------------------------------------------------------------------------
;		Public function definitions are defined here
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------

;----------------------------------------------------------------------------
;		Function prototype definitions are here
;----------------------------------------------------------------------------
USBMassCheckDeviceReady		PROTO NEAR SYSCALL
USBMassRequestSense 		PROTO NEAR SYSCALL
USBMassTestUnitReady 		PROTO NEAR SYSCALL
USBMassUpdateDeviceGeometry	PROTO NEAR SYSCALL
USBMassStartUnitCommand 	PROTO NEAR SYSCALL
USBMassInquiryCommand 		PROTO NEAR SYSCALL
USBMassModeSense		PROTO NEAR SYSCALL

USBMassIssueCBITransaction	PROTO NEAR SYSCALL
USBMassSendCBICommand		PROTO NEAR SYSCALL
USBMassGetCBIStatus		PROTO NEAR SYSCALL

USBMassIssueBOTTransaction	PROTO NEAR SYSCALL
USBMassSendBOTCommand		PROTO NEAR SYSCALL
USBMassGetBOTStatus		PROTO NEAR SYSCALL
USBMassBOTResetRecovery		PROTO NEAR SYSCALL

;----------------------------------------------------------------------------

;----------------------------------------------------------------------------
;	D A T A   S E G M E N T
;----------------------------------------------------------------------------
USB_DSEG	SEGMENT 	WORD PUBLIC 'DATA'
; BOTCommandTag used to maintain BOT command block number
BOTCommandTag			DWORD	?

wMassTempData			WORD	?

bUSBStorageDeviceDelayCount	BYTE	?

wBulkDataXferDelay		WORD	?

	PUBLIC	stMassXactStruc
stMassXactStruc		MASS_XACT_STRUC		< ? >

; Flag that allows mass storage device to handle special conditions. The 
; bit pattern is defined by the USBMASS_MISC_FLAG_XXX equates in USB.EQU
	PUBLIC	wMassStorageMiscFlag
wMassStorageMiscFlag		WORD	?

bGeometryCommandStatus		BYTE	?

bModeSenseSectors		BYTE	?
bModeSenseHeads			BYTE	?
wModeSenseCylinders		WORD	?
wModeSenseBlockSize		WORD	?
dModeSenseMaxLBA		DWORD	?

bReadCapSectors			BYTE	?
bReadCapHeads			BYTE	?
wReadCapCylinders		WORD	?
wReadCapBlockSize		WORD	?
dReadCapMaxLBA			DWORD	?

bDiskMediaType			BYTE	?

MODE_SENSE_COMMAND_EXECUTED		EQU		BIT0
READ_CAPACITY_COMMAND_EXECUTED		EQU		BIT1

;----------------------------------------------------------------------------
USB_DSEG	ENDS

;----------------------------------------------------------------------------
;	C O D E   S E G M E N T
;----------------------------------------------------------------------------
USB_CSEG	SEGMENT	WORD	USE16 	PUBLIC 'CODE'
	ASSUME	cs:USB_CSEG
	ASSUME	ds:USB_DSEG
.586p

	PUBLIC	_USBMASS_ASM_START
_USBMASS_ASM_START	LABEL		BYTE

	PUBLIC	USBMassDeviceHeader
USBMassDeviceHeader		LABEL		USB_DEV_HDR
	DB	BIOS_DEV_TYPE_STORAGE
	DW	OFFSET USBMassInitialize		; Init
	DW	OFFSET USBMassCheckForStorageDevice	; Identify
	DW	OFFSET USBMassConfigureStorageDevice	; Configure
	DW	OFFSET USBMassDisconnectStorageDevice	; Disconnect

;----------------------------------------------------------------------------
;		MASS STORAGE ENUMERATION PROCEDURE
;----------------------------------------------------------------------------

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMassInitialize
;
; Description:	This routine is called once to initialize the USB HID data
;		area.
;
; Input: 	Nothing
;
; Output: 	Nothing
;
; Modified:	Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMassInitialize	PROC NEAR SYSCALL PUBLIC

	push	ax
	push	cx

; Set default value for the delay
	mov	bUSBStorageDeviceDelayCount, 30	; Approx 15sec
; Read the delay setup option and store it in a local variable
	mov	ax, Q_USB_STORAGE_DEVICE_DELAY
	call	check_cmos_data_far
	inc	al
	mov	cl, 20
	mul	cl
; AX - 20, 40, 60 or 80
	mov	bUSBStorageDeviceDelayCount, al

	pop	cx
	pop	ax	
	ret
USBMassInitialize		ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMassCheckForStorageDevice
;
; Description:	This routine checks for mass storage type device from the
;		interface data provided
;
; Input: 	DL+	USB base class code
;		DH	USB sub-class code
;		DL	USB protocol code
;
; Output: 	AX	BIOS_DEV_TYPE_STORAGE type on success or 0FFH
;			on error
;
; Modified:	AX
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMassCheckForStorageDevice	PROC NEAR SYSCALL PUBLIC

	push	bx
	push	dx
	push	si

	xor	ax, ax
	mov	al, 0FFH		; Device type unknown

; If legacy mode is disabled. Do not enumerate mass storage device.
	test	dUSBInitFlag, USB_LEGACY_ENABLED_BIT
	jz	UMCFSD_Done
	
; If legacy mass support is disabled. Do not enumerate mass storage device.
	test	dUSBInitFlag, USB_LEGACY_STORAGE_BIT
	jz	UMCFSD_Done

	push	edx
	pop	dx
	pop	bx

; Check for BaseClass mass storage
	cmp	bl, BASE_CLASS_MASS_STORAGE
	jne	UMCFSD_Done	;Br if this interface is not a storage device

; Base class is okay. Check the protocol field for supported protocols.
; Currently we support CBI, CB and BOT protocols
	cmp	dl, PROTOCOL_CBI
	je	UMCFSD_MassProtocolOK
	cmp	dl, PROTOCOL_CBI_NO_INT
	je	UMCFSD_MassProtocolOK
	cmp	dl, PROTOCOL_BOT
	jne	UMCFSD_Done
UMCFSD_MassProtocolOk:
; Initialize mass storage specific entries in MassDeviceEntry[0]
	mov	si, OFFSET MassDeviceInfoTable
; Store the protocol implemented by the device
	mov     (MassDeviceInfo PTR [si]).bProtocol, dl
; Store the Command Block Specification in use by the device
	mov     (MassDeviceInfo PTR [si]).bSubClass, dh

	mov	ax, BIOS_DEV_TYPE_STORAGE
UMCFSD_Done:

	pop	si
	pop	dx
	pop	bx
	ret
USBMassCheckForStorageDevice	ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMassGetFreeMassDeviceInfoStruc
;
; Description:	This function finds a free mass device info structure and
;		returns the pointer to it
;
; Input: 	None
;
; Output: 	SI	Pointer to the Mass Device Info (0 on failure)
;		AX	MassDeviceInfo structure index
;
; Modified:	SI, AX
;
; Referrals:	MassDeviceInfo
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMassGetFreeMassDeviceInfoStruc	PROC NEAR SYSCALL PUBLIC

; Create a new MassDeviceInfo structure
	mov	si, OFFSET MassDeviceInfoTable
	add	si, SIZE MassDeviceInfo

; Get the mass device number (0 based)
	xor	ax, ax
	
umgfmdis_ProcessNextEntry:
	cmp	(MassDeviceInfo PTR [si]).bPresent, TRUE
	jne	umgfmdis_EntryFound
	inc	ax
	add	si, SIZE MassDeviceInfo
	cmp	si, OFFSET MassDeviceInfoTableEnd
	jb	umgfmdis_ProcessNextEntry

; No free entry found.  Return null
	xor	si, si

umgfmdis_EntryFound:

	ret
USBMassGetFreeMassDeviceInfoStruc	ENDP

;----------------------------------------------------------------------------
USBMassEmulationOptionTable		LABEL	WORD
	DW	Q_USB_MASS_DEV1_EMULATION
	DW	Q_USB_MASS_DEV2_EMULATION
	DW	Q_USB_MASS_DEV3_EMULATION
	DW	Q_USB_MASS_DEV4_EMULATION
	DW	Q_USB_MASS_DEV5_EMULATION
	DW	Q_USB_MASS_DEV6_EMULATION
USBMassEmulationOptionTableEnd		LABEL	WORD
;----------------------------------------------------------------------------

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMassFindFreeMassDeviceInfo
;
; Description:	This function finds a free mass device info structure and
;		copies the current mass device info structure into it
;
; Input: 	DI	Current mass device info structure
;
; Output: 	DI	New mass device info
;
; Modified:	DI
;
; Referrals:	MassDeviceInfo
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMassFindFreeMassDeviceInfo	PROC NEAR SYSCALL

	push	ax
	push	cx
	push	si
	push	es

; Get the free mass device info structure pointer
	call	USBMassGetFreeMassDeviceInfoStruc
	or	si, si
	jnz	UMCM_EntryFound

; No free entry found.  Return null
	xor	di, di
	jmp	SHORT UMFFMDI_Exit

UMCM_EntryFound:

; A free mass device info structure found. Copy the old one into the new one
	push	ds
	pop	es

	xchg	si, di
	cld
	push	di
	mov	cx, SIZE MassDeviceInfo
	rep	movsb
	pop	di

; Get the emulation type setup question associated with this device
	push	si
	mov	si, OFFSET USBMassEmulationOptionTable
	shl	ax, 1		; Multiply by 2
	add	si, ax
	mov	ax, WORD PTR cs:[si]
	pop	si
	mov	(MassDeviceInfo PTR [di]).wEmulationOption, ax

; Set default device type and emulation type to 0
	xor	ax, ax
	mov	(MassDeviceInfo PTR [di]).bDevType, al
	mov	(MassDeviceInfo PTR [di]).bEmuType, al
	mov	(MassDeviceInfo PTR [di]).pLUN0DevInfoPtr, ax

; Set block size to 0FFFFh
	mov	(MassDeviceInfo PTR [di]).wBlockSize, 0FFFFh
	mov	(MassDeviceInfo PTR [di]).bPresent, TRUE

UMFFMDI_Exit:
	pop	es
	pop	si
	pop	cx
	pop	ax
	ret
USBMassFindFreeMassDeviceInfo	ENDP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -