v4l2框架的虚拟视频驱动程序详解

dc2h_driver.c :

static int __init dc2h_init(void)
{
    int ret;

    struct dc2h_device *dev;
    struct video_device *vdev; 
    struct v4l2_ctrl_handler *hdl;
     struct vb2_queue *q;
    printk(KERN_EMERG "\n\n\n\n *****************    dc2h init module  *********************\n\n\n\n");
    //printk(KERN_EMERG"@@@@@ddc2h_init start@@@@\n");

    dev = kzalloc(sizeof(*dev), GFP_KERNEL);
    if(!dev)
    {   
        printk(KERN_EMERG "@@@@@dev is null @@@@\n");
        return -ENOMEM;
    }

    strcpy(dev->v4l2_dev.name, "/dev/dc2h_video");

    ret = v4l2_device_register(NULL, &dev->v4l2_dev);/*register the v4l2_device*/
    if( ret )
    {
        printk(KERN_EMERG "@@@@@v4l2_device_register fail@@@@\n");
        goto free_dev;
    }else{
        printk(KERN_EMERG "@@@@@v4l2_device_register success!@@@@\n");
    }

    /* initialize locks */
     spin_lock_init(&dev->slock);

    mutex_init(&dev->mutex);

     /* before register the video_device, init the video_device data*/
     ret = -ENOMEM;

     vdev = video_device_alloc();
     if(!vdev)
     {
        printk(KERN_EMERG "@@@@@video_device_alloc fail@@@@\n");
        goto unreg_dev;
     }else{
        printk(KERN_EMERG "@@@@@video_device_alloc success!@@@@\n");
    }

    *vdev = d2ch_tmplate; /* the most important struct */
    vdev->v4l2_dev = &dev->v4l2_dev; /* here set the v4l2_device, you have already registered it */
    //set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);

      vdev->lock = &dev->mutex;/*Provide a mutex to v4l2 core to protect all fops and v4l2 ioctls*/
    vdev->testflag = 123; //just test;!@#
    ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
    if (ret < 0) 
    {
        printk(KERN_EMERG "@@@@@video_register_device fail@@@@\n");
        goto rel_vdev;
    }else{
        printk(KERN_EMERG "@@@@@video_register_device success!@@@@\n");
    }
    printk(KERN_EMERG " @@@@@vdev->testflag-before =  %d\n", vdev->testflag);
    video_set_drvdata(vdev, dev);//!@#   set driver data

    dev->vdev = vdev; /*assoiate video_device and dc2h_device*/
    printk(KERN_EMERG " @@@@@vdev->testflag-after1 =  %d\n", vdev->testflag);
    /* the debug message*/
     v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", video_device_node_name(vdev));
    return 0; //  result : Unable to handle kernel NULL pointer dereference at virtual address 00000000
    //video_device_node_name

rel_vdev:
    video_device_release(vdev);

unreg_dev:
    v4l2_ctrl_handler_free(hdl);
    v4l2_device_unregister(&dev->v4l2_dev);

free_dev:
    kfree(dev);

    printk(KERN_EMERG " @@@@@vdev->testflag-after1 =  %d\n", vdev->testflag);
    return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值