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

📄 usb.h

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 H
📖 第 1 页 / 共 3 页
字号:
    USHORT              bcdDevice;
    UCHAR               iManufacturer;
    UCHAR               iProduct;
    UCHAR               iSerialNumber;
    UCHAR               bNumConfigurations;
    
} USB_DEVICE_DESC, *PUSB_DEVICE_DESC;


#define URB_FLAG_STATE_MASK  		0x0f
#define URB_FLAG_STATE_PENDING		0x00
#define URB_FLAG_STATE_IN_PROCESS   0x01
#define URB_FLAG_STATE_FINISHED     0x02

// USB2.0 state
#define URB_FLAG_STATE_DOORBELL		0x03	// for async request removal
#define URB_FLAG_STATE_WAIT_FRAME	0x04	// for sync request removal( for cancel only )
#define URB_FLAG_STATE_ERROR		0x05

#define URB_FLAG_IN_SCHEDULE        0x10
#define URB_FLAG_FORCE_CANCEL       0x20
#define URB_FLAG_SHORT_PACKET       0x80000000

typedef struct _SPLIT_ISO_BUS_TIME
{
	USHORT 				bus_time;
	USHORT				start_uframe;		

} SPLIT_ISO_BUS_TIME, *PSPLIT_ISO_BUS_TIME;

typedef struct _ISO_PACKET_DESC
{
	LONG 				offset;
	LONG 				length;		// expected length
	LONG 				actual_length;
	LONG 				status;
	union
	{
		LONG  				bus_time;   //opaque for client request of split iso, the bus_time is the start_uframe for each transaction
		SPLIT_ISO_BUS_TIME 	params;
	};

} ISO_PACKET_DESC, *PISO_PACKET_DESC;

#define CTRL_PARENT_URB_VALID	1

typedef void ( *PURBCOMPLETION )( struct _URB *purb, PVOID pcontext);

typedef struct _CTRL_REQ_STACK
{
	PURBCOMPLETION 		urb_completion;	// the last step of the urb_completion is to call the
										// the prevoius stack's callback if has, and the last
										// one is purb->completion
	PVOID  				context;
	ULONG 				params[ 3 ];

} CTRL_REQ_STACK, *PCTRL_REQ_STACK;

#pragma pack( pop, usb_align )

typedef struct _URB_HS_PIPE_CONTENT
{
	ULONG trans_type : 2;	// bit 0-1
	ULONG mult_count : 2;	// bit 2-3, used in high speed int and iso requests
	ULONG reserved : 1;		// bit 1
	ULONG speed_high : 1;	// bit 5
	ULONG speed_low : 1;	// bit 6
	ULONG trans_dir : 1;	// bit 7
	ULONG dev_addr : 7;		// bit 8-14
	ULONG endp_addr : 4;		// bit 15-18
	ULONG data_toggle : 1;	// bit 19
	ULONG max_packet_size : 4; // bit 20-23 log( max_packet_size )
	ULONG interval : 4;		// bit 24-27 the same definition in USB2.0, for high or full/low speed
	ULONG start_uframe : 3; // bit 28-30
	ULONG reserved1 : 1;    // 

} URB_HS_PIPE_CONTENT, *PURB_HS_PIPE_CONTENT;       

typedef struct _URB_HS_CONTEXT_CONTENT
{
	ULONG hub_addr : 7;	// high speed hub addr for split transfer
	ULONG port_idx : 7;
	ULONG reserved : 18;

} URB_HS_CONTEXT_CONTENT, *PURB_HS_CONTEXT_CONTENT;

typedef struct _URB
{
    LIST_ENTRY          urb_link;
    ULONG               flags;
	DEV_HANDLE 			endp_handle;
	LONG 				status;
		//record info for isr use, similar to td.status
		//int pipe has different content in the 8 msb
        //the eight bits contain interrupt interval.
        //and max packet length is encoded in 3 bits from 23-21
	    //that means 2^(x) bytes in the packet.
	ULONG 				pipe;               // bit0-1: endp type, bit 6: ls or fs. bit 7: dir
    
	union
	{
		UCHAR             	setup_packet[8];   	// for control
		LONG				params[ 2 ];		// params[ 0 ] is used in iso transfer as max_packet_size
	};

    PUCHAR              data_buffer;       	//user data
    LONG                data_length;       	//user data length

	struct _USB_DEV 	*pdev;
    struct _USB_ENDPOINT *pendp;             //pipe for current transfer

    PURBCOMPLETION      completion;
    PVOID               context;           	//parameter of completion

	PVOID				urb_ext;			//for high speed hcd use
    ULONG               hs_context;         //for high speed hcd use

	PIRP                pirp;           	//irp from client driver
	LONG  	            reference;          //for caller private use

	LONG        		td_count;           //for any kinds of transfer
    LONG                rest_bytes;         //for split bulk transfer involving more than 1024 tds
	LONG 				bytes_to_transfer;
	LONG          		bytes_transfered;	//( bulk transfer )accumulate one split-transfer by xfered bytes of the executed transactions
	PLIST_ENTRY         last_finished_td;   //last inactive td useful for large amount transfer
    LIST_ENTRY          trasac_list;        //list of tds or qhs

	union
	{
		LONG                iso_start_frame;	// for high speed endp, this is uframe index, and not used for full/low speed endp, instead,
												// iso_packet_desc.param.start_uframe is used.
		LONG				int_start_frame;	// frame( ms ) index for high/full/low speed endp
		struct
		{
			UCHAR			ctrl_req_flags;
			UCHAR			ctrl_stack_count;
			UCHAR			ctrl_cur_stack;		// the receiver uses this by increment the stack pointer first. if the requester
			UCHAR			ctrl_reserved;		// send it down with ctrl_stack_count zero, that means the stack is not initialized,
												// and can be initialized by receiver to 1 and only 1.
												// If the initializer found the stack size won't meet the number down the drivers, it must
												// reallocate one urb with the required stack size. and store the previous urb in 
												// ctrl_parent_urb
		} ctrl_req_context;
	};

	union
	{
		LONG 				iso_frame_count;	// uframe for high speed and frame for full/low speed
		struct _URB*		ctrl_parent_urb;
	};

	union
	{
		ISO_PACKET_DESC		iso_packet_desc[ 1 ]; 	//used to build up trasac_list for iso transfer and claim bandwidth
		CTRL_REQ_STACK		ctrl_req_stack[ 1 ];
	};
    
} URB, *PURB;


NTSTATUS
usb_set_dev_ext(
ULONG dev_ref,
PVOID dev_ext,
LONG size
);

NTSTATUS
usb_set_if_ext(
ULONG dev_ref,
ULONG if_ref,
PVOID if_ext,
LONG size
);

PVOID
usb_get_dev_ext(
ULONG dev_ref
);

PVOID
usb_get_if_ext(
ULONG dev_ref,
ULONG if_ref
);

NTSTATUS
usb_claim_interface(
ULONG dev_ref,
ULONG if_ref,
struct _USB_DRIVER *usb_drvr
);

//return reference to the endp
ULONG
usb_get_endp(
ULONG dev_ref,
LONG endp_addr
);

//return reference to the interface
ULONG
usb_get_interface(
ULONG dev_ref,
LONG if_idx
);

NTSTATUS
usb_set_configuration(
ULONG dev_ref,
LONG config_value
);

// each call will return full size of the config desc and
// its if, endp descs.
// return value is the bytes actually returned.
// if the return value is equal to wTotalLength, all the descs of the
// configuration returned.
NTSTATUS
usb_get_config_desc(
ULONG dev_ref,
LONG config_idx,
PCHAR buffer,
PLONG psize
);

NTSTATUS
usb_submit_urb(
struct _USB_DEV_MANAGER* dev_mgr,
PURB purb
);

NTSTATUS
usb_cancel_urb(
ULONG dev_ref,
ULONG endp_ref,
PURB purb
);

void usb_fill_int_urb(PURB urb,
    struct _USB_DEV *dev,
    ULONG pipe,
    PVOID transfer_buffer,
    LONG buffer_length,
    PURBCOMPLETION complete,
    PVOID context,
    int interval);

LONG
usb_calc_bus_time(
LONG low_speed,
LONG input_dir,
LONG isoc,
LONG bytecount
);

//increment the dev->ref_count to lock the dev
NTSTATUS
usb_query_and_lock_dev(
struct _USB_DEV_MANAGER* dev_mgr,
DEV_HANDLE dev_handle,
struct _USB_DEV** ppdev
);

//decrement the dev->ref_count
NTSTATUS
usb_unlock_dev(
struct _USB_DEV *dev
);

NTSTATUS
usb_reset_pipe(
struct _USB_DEV *pdev,
struct _USB_ENDPOINT *pendp,
PURBCOMPLETION client_completion,
PVOID param			//parameter for client_completion
);

VOID
usb_reset_pipe_completion(
PURB purb,
PVOID pcontext
);

PVOID
usb_alloc_mem(
POOL_TYPE pool_type,
LONG size
);

VOID
usb_free_mem(
PVOID pbuf
);

PUSB_CONFIGURATION_DESC
usb_find_config_desc_by_val(
PUCHAR pbuf,
LONG val,
LONG cfg_count
);

PUSB_CONFIGURATION_DESC
usb_find_config_desc_by_idx(
PUCHAR pbuf,
LONG idx,
LONG cfg_count
);

BOOLEAN
usb_skip_if_and_altif(
PUCHAR* pdesc_BUF
);

BOOLEAN
usb_skip_one_config(
PUCHAR* pconfig_desc_BUF
); 

VOID
usb_wait_ms_dpc(
ULONG ms
);

VOID
usb_wait_us_dpc(
ULONG us
);

BOOLEAN
usb_query_clicks(
PLARGE_INTEGER	clicks 
);

VOID
usb_cal_cpu_freq();

NTSTATUS
usb_reset_pipe_ex( 
struct _USB_DEV_MANAGER *dev_mgr,
DEV_HANDLE endp_handle,
PURBCOMPLETION reset_completion,
PVOID param
);

VOID
usb_call_ctrl_completion(
PURB purb
);

BOOLEAN
is_header_match(
PUCHAR pbuf,
ULONG type
); //used to check descriptor validity
#endif

⌨️ 快捷键说明

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