一个简单的驱动程序模板,在/dev目录下注册myDevDrv 。
应用程序使用open函数进行测试。
#include
#include
#include
#include
#includetypedef struct my_dev {
dispatch_t *dpp;
dispatch_context_t *ctp;
int id;
resmgr_connect_funcs_t *cfuncs;
resmgr_io_funcs_t *iofuncs;uint8_t *buf;
uint8_t *dmabuf;
unsigned buflen;void *drvhdl;
void *dlhdl;pthread_t tid;
int chid;
int coid;char *opts;
int devnum;void *next;
} my_dev_t;
// forward reference
int io_open (resmgr_context_t *, io_open_t *,RESMGR_HANDLE_T *, void *);int devCreateInstance(my_dev_t *dev);
void *my_driver_thread(void *data);
resmgr_connect_funcs_t connect_func;
resmgr_io_funcs_t io_func;my_dev_t gDev;
int main (int argc, char **argv)
{my_dev_t *pDev;
if (ThreadCtl(_NTO_TCTL_IO, 0) == -1) {
perror("ThreadCtl");
return (-1);
}
//pDev=malloc(sizeof(my_dev_t));pDev=&gDev;
devCreateInstance(pDev);
pthread_join(pDev->tid,NULL);
/////////////////////////////////////////////////////////////////////////
/*
* Load support .so library (dlopen,dlsym.....)
* Allocate memory for NULL_DEV_t;
* Register device instance
*/
/////////////////////////////////////////////////////////////////////////
}int io_open (resmgr_context_t *ctp, io_open_t *msg,RESMGR_HANDLE_T *handle, void *extra)
{
printf ("I'm here in the io_open!\n");
return (iofunc_open_default (ctp, msg, handle, extra));
}
int devCreateInstance(my_dev_t *dev)
{resmgr_attr_t resmgr_attr;
iofunc_attr_t attr;
pthread_attr_t pattr;
struct sched_param param;if (NULL == (dev->dpp = dispatch_create())) {
perror("dispatch_create() failed");
goto failed0;
}memset (&resmgr_attr, 0, sizeof (resmgr_attr));
resmgr_attr.nparts_max = 1;
resmgr_attr.msg_max_size = 2048;
// bind default functions into the outcall tables
iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &connect_func,
_RESMGR_IO_NFUNCS, &io_func);
iofunc_attr_init (&attr, S_IFNAM | 0666, 0, 0);// add the following to gain control:
connect_func.open = io_open;dev->cfuncs=&connect_func;
dev->iofuncs=&io_func;
printf("call resmgr_attach\n");
// establish a name in the pathname space
if (resmgr_attach (dev->dpp, &resmgr_attr, "/dev/myDevDrv",
_FTYPE_ANY, 0, &connect_func, &io_func, &attr) == -1) {
perror ("Unable to resmgr_attach");
exit (EXIT_FAILURE);
}
dev->ctp = dispatch_context_alloc (dev->dpp);
// wait here forever, handling messagespthread_attr_init(&pattr);
pthread_attr_setschedpolicy(&pattr, SCHED_RR);
param.sched_priority = 21;
pthread_attr_setschedparam(&pattr, ¶m);
pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED);printf("creater thread \n");
// Create thread for this interface
if (pthread_create(&dev->tid, &pattr, (void *)my_driver_thread, dev)< 0) {
perror("pthread_create() failed");
goto failed1;
}
return (0);failed1:
dispatch_destroy(dev->dpp);
failed0:return (-1);
}
void *my_driver_thread(void *data)
{
my_dev_t *dev = data;
while (1)
{
if ((dev->ctp = dispatch_block (dev->ctp)) == NULL) {
perror ("Unable to dispatch_block");
exit (EXIT_FAILURE);
}
dispatch_handler (dev->ctp);
}
}
测试用app程序
#include
#include
#include
#includeint main(int argc, char *argv[]) {
int drvFd;
drvFd=open("/dev/myDevDrv",0);
if(drvFd>0)
{
printf("Open driver successfully\n");
close(drvFd);
}return EXIT_SUCCESS;
}
运行结果