📄 usbcode.asm
字号:
asl a ; multiply index by 2 for jump table
cmp a, (end_class_desc_type_table - class_desc_type_table + 1)
jnc SendStall
jacc class_desc_type_table
XPAGEOFF
desc_type_table:
jmp SendStall ;no descriptor index 0
jmp GetDeviceDescriptor
jmp GetConfigurationDescriptor
jmp GetStringDescriptor
end_desc_type_table:
class_desc_type_table:
jmp SendStall ; no descriptor index 0
jmp GetHIDDescriptor
jmp GetReportDescriptor
jmp GetPhysicalDescriptor
end_class_desc_type_table:
XPAGEON
;===================================================================
; Get Descriptor = Device
;===================================================================
GetDeviceDescriptor:
mov A, (end_device_desc_table - device_desc_table)
mov [data_count], A ; save the device descriptor length
mov A, (device_desc_table - control_read_table)
jmp SendRomData ; pass a pointer to the device descriptor data
;===================================================================
; Get Descriptor = Config
;===================================================================
; When requested to send the config descritor all of the Class, Interface,
; HID and Endpoint descritors should be sent. For Draft 4 compliance, this
; order in mandatory
GetConfigurationDescriptor:
mov A, (end_config_desc_table - config_desc_table)
mov [data_count], A ; save the config descriptor length
mov A, (config_desc_table - control_read_table)
jmp SendRomData ; pass a pointer to the config descriptor data
;===================================================================
; Get Descriptor = String
;===================================================================
GetStringDescriptor:
mov A, [wValue] ; get string descriptor index
cmp a, (end_string_length_table - string_length_table + 1)
jnc SendStall ; check index is in range
index string_length_table ; get the descriptor length
mov [data_count], A ;
mov A, [wValue] ; get string descriptor index
index string_offset_table ; get a pointer to the string descriptor data
jmp SendRomData ;
XPAGEOFF
string_length_table:
db (USBStringDescription1 - USBStringLanguageDescription)
db (USBStringDescription2 - USBStringDescription1)
db (USBStringDescription3 - USBStringDescription2)
db (USBStringDescription4 - USBStringDescription3)
db (USBStringDescription5 - USBStringDescription4)
db (USBStringEnd - USBStringDescription5)
end_string_length_table:
string_offset_table:
db (USBStringLanguageDescription - control_read_table)
db (USBStringDescription1 - control_read_table)
db (USBStringDescription2 - control_read_table)
db (USBStringDescription3 - control_read_table)
db (USBStringDescription4 - control_read_table)
db (USBStringDescription5 - control_read_table)
end_string_offset_table:
XPAGEON
;===================================================================
; Get Descriptor = Report
;===================================================================
GetReportDescriptor:
mov A, (end_hid_report_desc_table - hid_report_desc_table)
mov [data_count], A ; save descriptor length
mov A, (hid_report_desc_table - control_read_table)
jmp SendRomData ; send descriptor to host
;===================================================================
; Get Descriptor = HID
;===================================================================
GetHIDDescriptor:
mov A, (Endpoint_Descriptor1 - Class_Descriptor)
mov [data_count], A ; save descriptor length
mov A, ( Class_Descriptor - control_read_table)
jmp SendRomData ; send descriptor to host
;===================================================================
; Get Descriptor = Physical
;===================================================================
; Physical descriptors are not supported by frameworks. Users wishing
; to support them should add code to send them here.
GetPhysicalDescriptor:
jmp SendStall ;not supported by frameworks
;=======================================================================================================
; Handle Request - Set Descriptor
;=======================================================================================================
; Set Descritor is not supported by frameworks. Users wishing to support this request should add code to
; do so here.
SetDescriptor: ;not supported by frameworks
jmp SendStall
;=======================================================================================================
; Handle Request - Get Configuration
;=======================================================================================================
; The only recipient supported for this request is Device
;
; Returns the current device configuration. Valid values are zero (unconfigured) and one (configured).
GetConfiguration:
mov a, [bmRequestType]
cmp a, DEVICE_TO_HOST
jnz SendStall
mov A, 1 ; send one byte
mov [data_count], A
mov A, (get_configuration_status_table - control_read_table)
add A, [configuration_status] ; get correct configuration
jmp SendRomData ; send configuration to host
;=======================================================================================================
; Handle Request - Get Configuration
;=======================================================================================================
; The only recipient supported for this request is Device
;
; Sets the configuration of the device to either unconfigured (0) or configured (1) based on wValue in the
; SETUP packet. USB spec rev 1.1 (para 9.1.1.5), requires Set Configuration to also clear the endpoint stall
; condition and re-initialize endpoints using data 0/1 toggle to Data0.
SetConfiguration:
mov a, [bmRequestType]
and a, HOST_TO_DEVICE ; faster than <cmp a, 00h>
jnz SendStall ; stall if recipient not Device
call NoDataControl ;handshake with host
mov A, [wValue]
mov [configuration_status], A ; store configuration byte
iord EP1_COUNT ; clear data 0/1 bit
and A, ~DATA01_B
iowr EP1_COUNT
iord EP1_MODE
and A, ~STALL_B
iowr EP1_MODE
mov A, [configuration_status]
and a, ffh ; faster than <cmp a, 00h>
jnz ConfigureDevice
UnconfigureDevice:
iord EP_IE
and A, ~EP1_IE ; disable endpoint 1
iowr EP1_IE
mov A, [interrupt_mask] ; disable endpoint one interrupts
and A, EFh
mov [interrupt_mask], A
ret
ConfigureDevice:
iord EP1_MODE ; NAK IN packets until data is
mov a, 0Eh
iowr EP1_MODE
iord EP_IE
or a, EP1_IE
iowr EP_IE ; enable endpoint 1
mov A, [interrupt_mask] ; enable gpio, ep1, 1ms 128us interrupts
or A, ENUMERATED_MASK
mov [interrupt_mask], A
iord USB_SCR ; NAK IN packets until data is
and A,0EFh ; ready on endpoint one
iowr USB_SCR
ret
;=======================================================================================================
; Handle Request - Get Interface
;=======================================================================================================
; The only recipient supported for this request is Interface
;
; The interface is an 8 bit value that is always zero since there are no alternate settings defined
GetInterface:
mov a, [bmRequestType]
cmp a, INTERFACE_TO_HOST
jnz SendStall
mov A, [wIndex] ;which interface ?
cmp A, 0 ;only interface 0 defined
jnz SendStall
mov A, 1 ;send one byte
mov [data_count], A
mov A, (get_interface_table - control_read_table)
jmp SendRomData ;send alternate interface to host
;=======================================================================================================
; Handle Request - Set Interface
;=======================================================================================================
; The only recipient supported for this request is Interface
;
; The host may attempt to set the interface to an alternative setting. The frameworks does not support any
; alternate setting, but user firmware may so we do not stall that request, though it would be legal to do so.
SetInterface:
mov a, [bmRequestType]
cmp a, DEVICE_TO_HOST
jnz SendStall
mov a,[wIndex] ; which interface ?
cmp a,0 ; only interface 0 defined
jnz SendStall
mov a,[wValue] ; which setting
cmp a, 0 ; only setting 0 supported
jnz SendStall
jmp NoDataControl ; ack the request
;=======================================================================================================
; Handle Request - Get Report
;=======================================================================================================
; This is a HID Class request. The only recipient supported is Interface
;
; Get Report allows the host to receive a report via the control pipe. The report type is specified in
; the wValue high byte while the low byte has a report ID.
GetReport:
mov a, [bmRequestType]
cmp a, CLASS + INTERFACE_TO_HOST
jnz SendStall
; Get Report support coming soon to a theater near you.....
jmp SendStall
;=======================================================================================================
; Handle Request - Get Idle
;=======================================================================================================
; This is a HID Class request. The only recipient supported is Interface
;
; Microsoft inform us that this request is not used. As it is optional, it is stalled.
GetIdle:
jmp SendStall
;=======================================================================================================
; Handle Request - Get Protocol
;=======================================================================================================
; This is a HID Class request. The only recipient supported is Interface
;
; Get Protocol sends the current protocol status back to the host. For boot protocol, wValue=0.
; For report protocol, wValue=1.
GetProtocol:
mov a, [bmRequestType]
cmp a, CLASS + INTERFACE_TO_HOST
jnz SendStall
mov A, 1 ; send one byte
mov [data_count], A
mov A, (get_protocol_status_table - control_read_table)
add A, [protocol_status] ; get correct configuration
jmp SendRomData
;=======================================================================================================
; Handle Request - Get Report
;=======================================================================================================
; This is a HID Class request. The only recipient supported is Interface
;
; Set Report allows the host to transmit a report via the control pipe. The report type is specified in
; the wValue high byte while the low byte has a report ID.
SetReport:
mov a, [bmRequestType]
cmp a, CLASS + HOST_TO_INTERFACE
jnz SendStall
; Set Report support coming soon to a theater near you.....
jmp SendStall
;=======================================================================================================
; Handle Request - Snet Idle
;=======================================================================================================
; This is a HID Class request. The only recipient supported is Interface
;
; Set Idle silences a particular report on the interrupt pipe until a new event occurs or the specified
; amount of time (wValue) passes.
;
; Microsoft inform us that this request is not used. As it is optional, it is stalled.
SetIdle:
jmp SendStall
;=======================================================================================================
; Handle Request - Get Protocol
;=======================================================================================================
; This is a HID Class request. The only recipient supported is Interface
;
; Set Protocol switches between the boot protocol and the report protocol. For boot protocol, wValue=0.
; For report protocol, wValue=1.
SetProtocol:
mov a, [bmRequestType]
cmp a, CLASS + HOST_TO_INTERFACE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -