diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c index 8440eafad44e..40fec8b025e8 100644 --- a/drivers/media/usb/em28xx/em28xx-cards.c +++ b/drivers/media/usb/em28xx/em28xx-cards.c @@ -3776,6 +3776,8 @@ static void em28xx_unregister_media_device(struct em28xx *dev) if (dev->media_dev) { media_device_unregister(dev->media_dev); media_device_cleanup(dev->media_dev); + if (dev->v4l2) + dev->v4l2->v4l2_dev.mdev = NULL; kfree(dev->media_dev); dev->media_dev = NULL; } diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c index 84424284d486..45b62655ca3b 100644 --- a/drivers/media/usb/em28xx/em28xx-video.c +++ b/drivers/media/usb/em28xx/em28xx-video.c @@ -2332,7 +2332,6 @@ static void em28xx_free_v4l2(struct v4l2_device *v4l2_dev) struct em28xx *dev = v4l2->dev; v4l2_ctrl_handler_free(&v4l2->ctrl_handler); - v4l2_device_unregister(v4l2_dev); dev->v4l2 = NULL; kfree(v4l2); kref_put(&dev->ref, em28xx_free_device); @@ -2367,7 +2366,7 @@ static int em28xx_v4l2_open(struct file *filp) return -ERESTARTSYS; v4l2 = dev->v4l2; - if (!v4l2) { + if (!v4l2 || dev->disconnected) { mutex_unlock(&dev->lock); return -ENODEV; } @@ -2462,7 +2461,7 @@ static int em28xx_v4l2_fini(struct em28xx *dev) * The em28xx_free_v4l2() release callback will be invoked once all * userspace file handles to the video device nodes are closed. */ - v4l2_device_put(&v4l2->v4l2_dev); + v4l2_device_unregister(&v4l2->v4l2_dev); return 0; }