MAC 下PCIE驱动模板

写一个MAC下的驱动。 遇坑无数,特此记录。

 

本例只是加载,无实际功能。 实现效果: 

 

 

MAC头文件

class com_osxkernel_MedCaptureDriver : public IOService
{
	OSDeclareDefaultStructors(com_osxkernel_MedCaptureDriver)
	
public:	
	virtual bool	init (OSDictionary* dictionary = NULL) override;
	virtual void	free (void) override;
	
	virtual IOService*	probe (IOService* provider, SInt32* score) override;
	virtual bool	start (IOService* provider) override;
	virtual void	stop (IOService* provider) override;
};

  

实现文件:

#include "MedCaptureDriver.hpp"
#include <IOKit/IOLib.h>

// Define the superclass
#define super IOService

OSDefineMetaClassAndStructors(com_osxkernel_MedCaptureDriver, IOService)

bool com_osxkernel_MedCaptureDriver::init (OSDictionary* dict)
{
	bool res = super::init(dict);
	IOLog("com_osxkernel_MedCaptureDriver::init\n");
	return res;
}

void com_osxkernel_MedCaptureDriver::free (void)
{
	IOLog("com_osxkernel_MedCaptureDriver::free\n");
	super::free();
}

IOService* com_osxkernel_MedCaptureDriver::probe (IOService* provider, SInt32* score)
{
	IOService *res = super::probe(provider, score);
	IOLog("com_osxkernel_MedCaptureDriver::probe\n");
	return res;
}

bool com_osxkernel_MedCaptureDriver::start (IOService *provider)
{
    IOLog("com_osxkernel_MedCaptureDriver::start\n");

    bool res = super::start(provider);
    setProperty("IOUserClientClass", "com_osxkernel_MedCaptureClient");
    registerService();
    return res;
}




void com_osxkernel_MedCaptureDriver::stop (IOService *provider)
{
	IOLog("com_osxkernel_MedCaptureDriver::stop\n");
	super::stop(provider);
}

  

用户头:

class com_osxkernel_MedCaptureClient : public IOUserClient
{
	OSDeclareDefaultStructors(com_osxkernel_MedCaptureClient)
	
private:
	task_t								m_task;
	com_osxkernel_MedCaptureDriver*		m_driver;
	
public:	
	virtual bool		initWithTask (task_t owningTask, void* securityToken, UInt32 type, OSDictionary* properties) override;
	virtual bool		start (IOService* provider) override;
	virtual void		stop (IOService* provider) override;
	virtual void		free (void) override;
	
    // 鎵╁睍鍛戒护...
	//virtual IOReturn	externalMethod (uint32_t selector, IOExternalMethodArguments* arguments, IOExternalMethodDispatch* dispatch = 0, OSObject* target = 0, void* reference = 0);
};

  

用户实现:

 

OSDefineMetaClassAndStructors(com_osxkernel_MedCaptureClient, IOUserClient)

bool com_osxkernel_MedCaptureClient::initWithTask (task_t owningTask, void* securityToken, UInt32 type, OSDictionary* properties)
{

	IOLog("com_osxkernel_MedCaptureClient::initWithTask\n");

    if(!owningTask) 
        return false;

    if (!super::initWithTask(owningTask, securityToken, type, properties)) {
        return false;
    }

    m_task = owningTask;

    //鏄惁鏈夌鐞嗘潈闄?.
	IOReturn ret = clientHasPrivilege(securityToken, kIOClientPrivilegeAdministrator);
	if ( ret == kIOReturnSuccess )
	{
		// m_taskIsAdmin = true;
	}



    return true;
    
}

bool com_osxkernel_MedCaptureClient::start (IOService* provider)
{
	IOLog("com_osxkernel_MedCaptureClient::start\n");

	if (! super::start(provider))
		return false;
	
	m_driver = OSDynamicCast(com_osxkernel_MedCaptureDriver, provider);
	if (!m_driver)
		return false;
	
	return true;
}

void com_osxkernel_MedCaptureClient::stop (IOService* provider)
{
	IOLog("com_osxkernel_MedCaptureClient::stop\n");
	super::stop(provider);
}

void com_osxkernel_MedCaptureClient::free (void)
{
	IOLog("com_osxkernel_MedCaptureClient::free\n");
	super::free();
}

  

 

posted @ 2021-07-27 15:28  睡觉的虫  阅读(191)  评论(0编辑  收藏  举报