Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9832e155 authored by Javier Martinez Canillas's avatar Javier Martinez Canillas Committed by Mauro Carvalho Chehab
Browse files

[media] media-device: split media initialization and registration



The media device node is registered and so made visible to user-space
before entities are registered and links created which means that the
media graph obtained by user-space could be only partially enumerated
if that happens too early before all the graph has been created.

To avoid this race condition, split the media init and registration
in separate functions and only register the media device node when
all the pending subdevices have been registered, either explicitly
by the driver or asynchronously using v4l2_async_register_subdev().

The media_device_register() had a check for drivers not filling dev
and model fields but all drivers in mainline set them and not doing
it will be a driver bug so change the function return to void and
add a BUG_ON() for dev being NULL instead.

Also, add a media_device_cleanup() function that will destroy the
graph_mutex that is initialized in media_device_init().

[mchehab@osg.samsung.com: Fix compilation if !CONFIG_MEDIA_CONTROLLER
 and remove two warnings added by this changeset]
Suggested-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: default avatarJavier Martinez Canillas <javier@osg.samsung.com>
Acked-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent 821ed366
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -617,6 +617,7 @@ static void smsdvb_media_device_unregister(struct smsdvb_client_t *client)
	if (!coredev->media_dev)
		return;
	media_device_unregister(coredev->media_dev);
	media_device_cleanup(coredev->media_dev);
	kfree(coredev->media_dev);
	coredev->media_dev = NULL;
#endif
+29 −8
Original line number Diff line number Diff line
@@ -616,7 +616,7 @@ EXPORT_SYMBOL_GPL(media_device_unregister_entity);


/**
 * media_device_register - register a media device
 * media_device_init() - initialize a media device
 * @mdev:	The media device
 *
 * The caller is responsible for initializing the media device before
@@ -625,14 +625,8 @@ EXPORT_SYMBOL_GPL(media_device_unregister_entity);
 * - dev must point to the parent device
 * - model must be filled with the device model name
 */
int __must_check __media_device_register(struct media_device *mdev,
					 struct module *owner)
void media_device_init(struct media_device *mdev)
{
	int ret;

	if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
		return -EINVAL;

	INIT_LIST_HEAD(&mdev->entities);
	INIT_LIST_HEAD(&mdev->interfaces);
	INIT_LIST_HEAD(&mdev->pads);
@@ -640,6 +634,33 @@ int __must_check __media_device_register(struct media_device *mdev,
	spin_lock_init(&mdev->lock);
	mutex_init(&mdev->graph_mutex);

	dev_dbg(mdev->dev, "Media device initialized\n");
}
EXPORT_SYMBOL_GPL(media_device_init);

/**
 * media_device_cleanup() - Cleanup a media device
 * @mdev:	The media device
 *
 */
void media_device_cleanup(struct media_device *mdev)
{
	mutex_destroy(&mdev->graph_mutex);
}
EXPORT_SYMBOL_GPL(media_device_cleanup);

/**
 * __media_device_register() - register a media device
 * @mdev:	The media device
 * @owner:	The module owner
 *
 * returns zero on success or a negative error code.
 */
int __must_check __media_device_register(struct media_device *mdev,
					 struct module *owner)
{
	int ret;

	/* Register the device node. */
	mdev->devnode.fops = &media_device_fops;
	mdev->devnode.parent = mdev->dev;
+7 −8
Original line number Diff line number Diff line
@@ -1313,7 +1313,10 @@ static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
unlock:
	mutex_unlock(&fmd->media_dev.graph_mutex);
	if (ret < 0)
		return ret;

	return media_device_register(&fmd->media_dev);
}

static int fimc_md_probe(struct platform_device *pdev)
@@ -1350,11 +1353,7 @@ static int fimc_md_probe(struct platform_device *pdev)
		return ret;
	}

	ret = media_device_register(&fmd->media_dev);
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
		goto err_v4l2_dev;
	}
	media_device_init(&fmd->media_dev);

	ret = fimc_md_get_clocks(fmd);
	if (ret)
@@ -1424,8 +1423,7 @@ static int fimc_md_probe(struct platform_device *pdev)
err_m_ent:
	fimc_md_unregister_entities(fmd);
err_md:
	media_device_unregister(&fmd->media_dev);
err_v4l2_dev:
	media_device_cleanup(&fmd->media_dev);
	v4l2_device_unregister(&fmd->v4l2_dev);
	return ret;
}
@@ -1445,6 +1443,7 @@ static int fimc_md_remove(struct platform_device *pdev)
	fimc_md_unregister_entities(fmd);
	fimc_md_pipelines_free(fmd);
	media_device_unregister(&fmd->media_dev);
	media_device_cleanup(&fmd->media_dev);
	fimc_md_put_clocks(fmd);

	return 0;
+7 −7
Original line number Diff line number Diff line
@@ -1793,6 +1793,7 @@ static void isp_unregister_entities(struct isp_device *isp)

	v4l2_device_unregister(&isp->v4l2_dev);
	media_device_unregister(&isp->media_dev);
	media_device_cleanup(&isp->media_dev);
}

static int isp_link_entity(
@@ -1875,12 +1876,7 @@ static int isp_register_entities(struct isp_device *isp)
		sizeof(isp->media_dev.model));
	isp->media_dev.hw_revision = isp->revision;
	isp->media_dev.link_notify = isp_pipeline_link_notify;
	ret = media_device_register(&isp->media_dev);
	if (ret < 0) {
		dev_err(isp->dev, "%s: Media device registration failed (%d)\n",
			__func__, ret);
		return ret;
	}
	media_device_init(&isp->media_dev);

	isp->v4l2_dev.mdev = &isp->media_dev;
	ret = v4l2_device_register(isp->dev, &isp->v4l2_dev);
@@ -2365,7 +2361,11 @@ static int isp_subdev_notifier_complete(struct v4l2_async_notifier *async)
		}
	}

	return v4l2_device_register_subdev_nodes(v4l2_dev);
	ret = v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
	if (ret < 0)
		return ret;

	return media_device_register(&isp->media_dev);
}

/*
+10 −5
Original line number Diff line number Diff line
@@ -305,7 +305,7 @@ static void camif_unregister_media_entities(struct camif_dev *camif)
/*
 * Media device
 */
static int camif_media_dev_register(struct camif_dev *camif)
static int camif_media_dev_init(struct camif_dev *camif)
{
	struct media_device *md = &camif->media_dev;
	struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
@@ -328,9 +328,7 @@ static int camif_media_dev_register(struct camif_dev *camif)
	if (ret < 0)
		return ret;

	ret = media_device_register(md);
	if (ret < 0)
		v4l2_device_unregister(v4l2_dev);
	media_device_init(md);

	return ret;
}
@@ -483,7 +481,7 @@ static int s3c_camif_probe(struct platform_device *pdev)
		goto err_alloc;
	}

	ret = camif_media_dev_register(camif);
	ret = camif_media_dev_init(camif);
	if (ret < 0)
		goto err_mdev;

@@ -510,6 +508,11 @@ static int s3c_camif_probe(struct platform_device *pdev)
		goto err_unlock;

	mutex_unlock(&camif->media_dev.graph_mutex);

	ret = media_device_register(&camif->media_dev);
	if (ret < 0)
		goto err_sens;

	pm_runtime_put(dev);
	return 0;

@@ -518,6 +521,7 @@ static int s3c_camif_probe(struct platform_device *pdev)
err_sens:
	v4l2_device_unregister(&camif->v4l2_dev);
	media_device_unregister(&camif->media_dev);
	media_device_cleanup(&camif->media_dev);
	camif_unregister_media_entities(camif);
err_mdev:
	vb2_dma_contig_cleanup_ctx(camif->alloc_ctx);
@@ -539,6 +543,7 @@ static int s3c_camif_remove(struct platform_device *pdev)
	struct s3c_camif_plat_data *pdata = &camif->pdata;

	media_device_unregister(&camif->media_dev);
	media_device_cleanup(&camif->media_dev);
	camif_unregister_media_entities(camif);
	v4l2_device_unregister(&camif->v4l2_dev);

Loading