动态加载uvc驱动,video节点固定

之前有客户需求,提供接口给应用层动态注册/注销uvc驱动,一般的操作可以把uvc编译成ko文件,应用层可以获取系统权限,使用su命令进行insmod uvc.ko加载驱动,rm uvc.ko卸载驱动。由于客户不想使用该方案,因此大概看了一下代码,自己做了接口提供给客户,以下为代码参考:


+extern void app_unregister_uvc(void);
+extern int app_register_uvc(void);
 extern int now_lcd_is_on;
 
  struct input_dev *input_gl;
@@ -112,24 +114,24 @@ static ssize_t body_store(struct class *cls,struct class_attribute *attr, const
        int  dbg_thresd = simple_strtol(buf,NULL,10);
         if(dbg_thresd  == 111)
         {
-            open_flag = 0;
-            printk("turn off report\n");
+            app_register_uvc();
+            printk("app_register_uvc\n");
         }
         else if (dbg_thresd == 222)
         {
-            open_flag = 1;
-            printk("turn on report");
+            app_unregister_uvc();  
+            printk("app_unregister_uvc\n");
         }
 #endif
     return _count;
 }
-static CLASS_ATTR(body, 0664, body_show, body_store);
+static CLASS_ATTR(rw, 0664, body_show, body_store);
 
 static int body_sys_init(void)
 {
         int ret ;
-        body_class = class_create(THIS_MODULE, "gpio_read");
-        ret =  class_create_file(body_class, &class_attr_body);
+        body_class = class_create(THIS_MODULE, "uvc_ctrl");
+        ret =  class_create_file(body_class, &class_attr_rw);
     if (ret)
     {
        printk("Fail to creat class hkrkfb.\n");
diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index 435b2ee..92d7df6 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -39,7 +39,8 @@ unsigned int uvc_no_drop_param;
 static unsigned int uvc_quirks_param = -1;
 unsigned int uvc_trace_param;
 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
-
+struct usb_interface *Ap_intf;
+struct uvc_device *Ap_dev;
 /* ------------------------------------------------------------------------
  * Video formats
  */
@@ -2020,6 +2021,8 @@ static int uvc_probe(struct usb_interface *intf,
 {
     struct usb_device *udev = interface_to_usbdev(intf);
     struct uvc_device *dev;
+    Ap_dev = dev;
+    Ap_intf = intf;
     const struct uvc_device_info *info =
         (const struct uvc_device_info *)id->driver_info;
     u32 quirks = info ? info->quirks : 0;
@@ -2149,6 +2152,30 @@ static void uvc_disconnect(struct usb_interface *intf)
     uvc_unregister_video(dev);
 }
 
+
+
+void app_unregister_uvc(void)
+{
+    //struct uvc_device *dev = usb_get_intfdata(Ap_intf);
+    //uvc_unregister_video(Ap_dev);
+    usb_deregister(&uvc_driver.driver);
+}
+
+
+int app_register_uvc(void)
+{
+    int ret;
+    uvc_debugfs_init();
+    ret = usb_register(&uvc_driver.driver);
+    if (ret < 0) {
+        uvc_debugfs_cleanup();
+        return ret;
+    }
+
+    printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
+    return 0;
+}
+
 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
 {
     struct uvc_device *dev = usb_get_intfdata(intf);
diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
index 72a8b80..74e2d83 100644
--- a/drivers/media/v4l2-core/v4l2-dev.c
+++ b/drivers/media/v4l2-core/v4l2-dev.c
@@ -975,7 +975,10 @@ void video_unregister_device(struct video_device *vdev)
      */
     clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
     mutex_unlock(&videodev_lock);
+    /*******clear device node after plug out****/
+    devnode_clear(vdev);//added by hxl ,每次重新加载后,生成的节点都是/dev/video0,否则重新加载一次会是/dev/videoX
     device_unregister(&vdev->dev);
+    printk("hxl video_unregister_device ==\n");
 }
 EXPORT_SYMBOL(video_unregister_device);
 
 

 

你可能感兴趣的:(动态加载uvc驱动,video节点固定)