短语解释:
HAL:Hardware Abstraction Layer
DAL:Device Abstraction Layer
DDI:Device Driver Interface
HAL层定义底层硬件操作,而DAL层位于驱动和应用层之间,DAL API定义在DDI文件中,应用层调用DDI接口使用驱动提供的服务;
1. DAL框架层
DAL层为每个设备分配一个DeviceId,应用层使用Attach方法把DeviceId和DalDeviceHandle关联在一起;
DalDeviceHandle结构体定义如下:
struct DalDeviceHandle
{
uint32 dwDalHandleId;
const DalInterface *pVtbl;
void *pClientCtxt;
};
@dwDalHandleId使用预设值
DALDEVICE_INTERFACE_HANDLE_ID;
@pVtbl是DalInterface类型成员,定义设备通用方法和自定义方法;
@pClientCtxt指向客户端上下文变量;
DalInterface结构体定义如下:
struct DalInterface
{
struct DalDevice DalDevice;
//User will add their prototypes here.
};
DalDevice结构体定义通用方法,用户自定义方法可以添加到DalInterface中;
struct DalDevice
{
DALResult (*Attach)(const char*,DALDEVICEID,DalDeviceHandle **);
uint32 (*Detach)(DalDeviceHandle *);
DALResult (*Init)(DalDeviceHandle *);
DALResult (*DeInit)(DalDeviceHandle *);
DALResult (*Open)(DalDeviceHandle *, uint32);
DALResult (*Close)(DalDeviceHandle *);
DALResult (*Info)(DalDeviceHandle *, DalDeviceInfo *, uint32);
DALResult (*PowerEvent)(DalDeviceHandle *, DalPowerCmd, DalPowerDomain);
DALResult (*SysRequest)(DalDeviceHandle *, DalSysReq, const void *, uint32, void *,uint32, uint32*);
};
DAL层定义了一系列以"DalDevice_*"开头的帮助函数来调用这些通用方法;
DALResult DalDevice_Init(DalDeviceHandle *_h)
DALResult DalDevice_DeInit(DalDeviceHandle *_h)
DALResult DalDevice_Open(DalDeviceHandle *_h, uint32 mode)
DALResult DalDevice_Close(DalDeviceHandle *_h)
DAL层还定义了DAL_DeviceAttach方法,查找与DeviceId关联的DalDeviceHandle,如果是第一次执行Attach方法,还需要执行
DalDevice_Init操作 :
DALResult DAL_DeviceAttach( DALDEVICEID DevId, DalDeviceHandle **phDalDevice)
{
DALResult ret = _DALSYSCMN_GetDeviceObject(DevId, NULL, phDalDevice);
if(DAL_SUCCESS == ret)
{
//if a remote handle the remote side does the Init
if(!DALISREMOTEHANDLE(*phDalDevice))
{
if(1 == DAL_GET_DEVICE_ATTACH_COUNT(*phDalDevice))
{
ret = DalDevice_Init(*phDalDevice);
if(DAL_SUCCESS != ret)
{
DAL_DeviceDetach(*phDalDevice);
*phDalDevice = NULL;
}
}
}
}
return ret;
}
_DALSYSCMN_GetDeviceObjec方法从
gDALProcessDriverModList数组查找DalDeviceHandle,找到以后会调用设备定义的Attach函数;
static DALResult _DALSYSCMN_GetDeviceObject(DALDEVICEID DevId, const char *pszArg, DalDeviceHandle **phDevice)
{
DALREG_DriverInfoList *pModDriverInfoList = gDALProcessDriverModList[0];
uint32 dwDriverIdx = 0;
*phDevice = NULL;
for(dwDriverIdx =0;dwDriverIdx<pModDriverInfoList->dwLen;dwDriverIdx++)
{
uint32 dwDeviceIdx = 0;
DALREG_DriverInfo *pDriverInfo = pModDriverInfoList->pDriverInfo[dwDriverIdx];
for(dwDeviceIdx=0;dwDeviceIdx<pDriverInfo->dwNumDevices;dwDeviceIdx++)
{
if(DevId == pDriverInfo->pDeviceId[dwDeviceIdx])
return
pDriverInfo->pfnDALNewFunc(pszArg,DevId,phDevice);
}
}
return D