Wednesday, September 05, 2007

Windows Driver programming[6]

前面提到,DRIVER_OBJECT 的 MajorFunction function pointer array。這玩意兒要怎麼填?先來看看 IRP_MJ_MAXIMUM_FUNCTION 及其相關的定義吧:

#define IRP_MJ_CREATE                             0x00
#define IRP_MJ_CREATE_NAMED_PIPE                  0x01
#define IRP_MJ_CLOSE                              0x02
#define IRP_MJ_READ                               0x03
#define IRP_MJ_WRITE                              0x04
#define IRP_MJ_QUERY_INFORMATION                  0x05
#define IRP_MJ_SET_INFORMATION                    0x06
#define IRP_MJ_QUERY_EA                           0x07
#define IRP_MJ_SET_EA                             0x08
#define IRP_MJ_FLUSH_BUFFERS                      0x09
#define IRP_MJ_QUERY_VOLUME_INFORMATION           0x0a
#define IRP_MJ_SET_VOLUME_INFORMATION             0x0b
#define IRP_MJ_DIRECTORY_CONTROL                  0x0c
#define IRP_MJ_FILE_SYSTEM_CONTROL                0x0d
#define IRP_MJ_DEVICE_CONTROL                     0x0e
#define IRP_MJ_INTERNAL_DEVICE_CONTROL            0x0f
#define IRP_MJ_SHUTDOWN                           0x10
#define IRP_MJ_LOCK_CONTROL                       0x11
#define IRP_MJ_CLEANUP                            0x12
#define IRP_MJ_CREATE_MAILSLOT                    0x13
#define IRP_MJ_QUERY_SECURITY                     0x14
#define IRP_MJ_SET_SECURITY                       0x15
#define IRP_MJ_POWER                              0x16
#define IRP_MJ_SYSTEM_CONTROL                     0x17
#define IRP_MJ_DEVICE_CHANGE                      0x18
#define IRP_MJ_QUERY_QUOTA                        0x19
#define IRP_MJ_SET_QUOTA                          0x1a
#define IRP_MJ_PNP                                0x1b
#define IRP_MJ_PNP_POWER                          IRP_MJ_PNP      // Obsolete....
#define IRP_MJ_MAXIMUM_FUNCTION                   0x1b


IRP_MJ_MAXIMUM_FUNCTION 的定義會依據平台不同而有所不同。MajorFunction,顧名思義,就是定義 major function,而每個 major function 裡面還有一堆 minor function(IRP_MN_XXX) 可以實做,喔天啊。不過幸好一個 driver 只需要實做它需要的 major function 就好了。比較重要的當然是 IRP_MJ_READ/IRP_MJ_WRITE 啦。IRP_MJ_POWER 牽涉到電源管理,很重要,但是在 driver 開發前中期是不會去管它的。至於這些 major function 的詳細說明,請參考 DDK 文件,非常詳細。

注意一下,MajorFunction 的型別:

typedef NTSTATUS (*PDRIVER_DISPATCH) (
    IN struct _DEVICE_OBJECT *DeviceObject,
    IN struct _IRP *Irp
);


傳入值一定是 DEVICE_OBJECT 跟 IRP。傳入 DEVICE_OBJECT 是為了取得 DeviceExtension,而 DEVICE_OBJECT 是透過 IoAttachDeviceToDeviceStack() 註冊到 IO Manager,環環相扣,搞清楚就不會摸不著頭緒了。 而 IRP 當然就是希望 driver 處理的東西啦。

至於 IRP 怎麼處理嘛,下一節再說吧。Programming the microsoft windows driver model 花了一整章在說明 IRP 的處理方式耶。

No comments:

codeblock