[PATCH v2 02/17] tee: Add probe, remove and shutdown bus callbacks to tee_client_driver

Uwe Kleine-König posted 17 patches 1 month, 3 weeks ago
[PATCH v2 02/17] tee: Add probe, remove and shutdown bus callbacks to tee_client_driver
Posted by Uwe Kleine-König 1 month, 3 weeks ago
Introduce a bus specific probe, remove and shutdown function. For now
this only allows to get rid of a cast of the generic device to a
tee_client device in the drivers and changes the remove prototype to
return void---a non-zero return value is ignored anyhow.

The objective is to get rid of users of struct device_driver callbacks
.probe(), .remove() and .shutdown() to eventually remove these. Until
all tee_client drivers are converted this results in a runtime warning
about the drivers needing an update because there is a bus probe
function and a driver probe function.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/tee/tee_core.c  | 68 +++++++++++++++++++++++++++++++++++++++++
 include/linux/tee_drv.h |  3 ++
 2 files changed, 71 insertions(+)

diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
index 51379f7fc5d5..a015730664c7 100644
--- a/drivers/tee/tee_core.c
+++ b/drivers/tee/tee_core.c
@@ -1398,19 +1398,87 @@ static int tee_client_device_uevent(const struct device *dev,
 	return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
 }
 
+static int tee_client_device_probe(struct device *dev)
+{
+	struct tee_client_device *tcdev = to_tee_client_device(dev);
+	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
+
+	if (drv->probe)
+		return drv->probe(tcdev);
+	else
+		return 0;
+}
+
+static void tee_client_device_remove(struct device *dev)
+{
+	struct tee_client_device *tcdev = to_tee_client_device(dev);
+	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
+
+	if (drv->remove)
+		drv->remove(tcdev);
+}
+
+static void tee_client_device_shutdown(struct device *dev)
+{
+	struct tee_client_device *tcdev = to_tee_client_device(dev);
+	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
+
+	if (dev->driver && drv->shutdown)
+		drv->shutdown(tcdev);
+}
+
 const struct bus_type tee_bus_type = {
 	.name		= "tee",
 	.match		= tee_client_device_match,
 	.uevent		= tee_client_device_uevent,
+	.probe		= tee_client_device_probe,
+	.remove		= tee_client_device_remove,
+	.shutdown	= tee_client_device_shutdown,
 };
 EXPORT_SYMBOL_GPL(tee_bus_type);
 
+static int tee_client_device_probe_legacy(struct tee_client_device *tcdev)
+{
+	struct device *dev = &tcdev->dev;
+	struct device_driver *driver = dev->driver;
+
+	return driver->probe(dev);
+}
+
+static void tee_client_device_remove_legacy(struct tee_client_device *tcdev)
+{
+	struct device *dev = &tcdev->dev;
+	struct device_driver *driver = dev->driver;
+
+	driver->remove(dev);
+}
+
+static void tee_client_device_shutdown_legacy(struct tee_client_device *tcdev)
+{
+	struct device *dev = &tcdev->dev;
+	struct device_driver *driver = dev->driver;
+
+	driver->shutdown(dev);
+}
+
 int __tee_client_driver_register(struct tee_client_driver *tee_driver,
 				 struct module *owner)
 {
 	tee_driver->driver.owner = owner;
 	tee_driver->driver.bus = &tee_bus_type;
 
+	/*
+	 * Drivers that have callbacks set for tee_driver->driver need updating
+	 * to use the callbacks in tee_driver instead. driver_register() warns
+	 * about that, so no need to warn here, too.
+	 */
+	if (!tee_driver->probe && tee_driver->driver.probe)
+		tee_driver->probe = tee_client_device_probe_legacy;
+	if (!tee_driver->remove && tee_driver->driver.remove)
+		tee_driver->remove = tee_client_device_remove_legacy;
+	if (!tee_driver->shutdown && tee_driver->driver.probe)
+		tee_driver->shutdown = tee_client_device_shutdown_legacy;
+
 	return driver_register(&tee_driver->driver);
 }
 EXPORT_SYMBOL_GPL(__tee_client_driver_register);
diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h
index 850c03b2cdea..e561a26f537a 100644
--- a/include/linux/tee_drv.h
+++ b/include/linux/tee_drv.h
@@ -315,6 +315,9 @@ struct tee_client_device {
  * @driver:		driver structure
  */
 struct tee_client_driver {
+	int (*probe)(struct tee_client_device *);
+	void (*remove)(struct tee_client_device *);
+	void (*shutdown)(struct tee_client_device *);
 	const struct tee_client_device_id *id_table;
 	struct device_driver driver;
 };
-- 
2.47.3

Re: [PATCH v2 02/17] tee: Add probe, remove and shutdown bus callbacks to tee_client_driver
Posted by Sumit Garg 1 month, 3 weeks ago
On Mon, Dec 15, 2025 at 03:16:32PM +0100, Uwe Kleine-König wrote:
> Introduce a bus specific probe, remove and shutdown function. For now
> this only allows to get rid of a cast of the generic device to a
> tee_client device in the drivers and changes the remove prototype to
> return void---a non-zero return value is ignored anyhow.
> 
> The objective is to get rid of users of struct device_driver callbacks
> .probe(), .remove() and .shutdown() to eventually remove these. Until
> all tee_client drivers are converted this results in a runtime warning
> about the drivers needing an update because there is a bus probe
> function and a driver probe function.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> ---
>  drivers/tee/tee_core.c  | 68 +++++++++++++++++++++++++++++++++++++++++
>  include/linux/tee_drv.h |  3 ++
>  2 files changed, 71 insertions(+)
> 
> diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> index 51379f7fc5d5..a015730664c7 100644
> --- a/drivers/tee/tee_core.c
> +++ b/drivers/tee/tee_core.c
> @@ -1398,19 +1398,87 @@ static int tee_client_device_uevent(const struct device *dev,
>  	return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
>  }
>  
> +static int tee_client_device_probe(struct device *dev)
> +{
> +	struct tee_client_device *tcdev = to_tee_client_device(dev);
> +	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
> +
> +	if (drv->probe)
> +		return drv->probe(tcdev);
> +	else
> +		return 0;
> +}
> +
> +static void tee_client_device_remove(struct device *dev)
> +{
> +	struct tee_client_device *tcdev = to_tee_client_device(dev);
> +	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
> +
> +	if (drv->remove)
> +		drv->remove(tcdev);
> +}
> +
> +static void tee_client_device_shutdown(struct device *dev)
> +{
> +	struct tee_client_device *tcdev = to_tee_client_device(dev);
> +	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
> +
> +	if (dev->driver && drv->shutdown)

Is check for dev->driver really needed here? I think we only reach this
stage if a driver is associated to the device. Also, it has been
dereferenced above too.

Apart from this, rest looks fine to me. Feel free to add:

Reviewed-by: Sumit Garg <sumit.garg@oss.qualcomm.com>

-Sumit

> +		drv->shutdown(tcdev);
> +}
> +
>  const struct bus_type tee_bus_type = {
>  	.name		= "tee",
>  	.match		= tee_client_device_match,
>  	.uevent		= tee_client_device_uevent,
> +	.probe		= tee_client_device_probe,
> +	.remove		= tee_client_device_remove,
> +	.shutdown	= tee_client_device_shutdown,
>  };
>  EXPORT_SYMBOL_GPL(tee_bus_type);
>  
> +static int tee_client_device_probe_legacy(struct tee_client_device *tcdev)
> +{
> +	struct device *dev = &tcdev->dev;
> +	struct device_driver *driver = dev->driver;
> +
> +	return driver->probe(dev);
> +}
> +
> +static void tee_client_device_remove_legacy(struct tee_client_device *tcdev)
> +{
> +	struct device *dev = &tcdev->dev;
> +	struct device_driver *driver = dev->driver;
> +
> +	driver->remove(dev);
> +}
> +
> +static void tee_client_device_shutdown_legacy(struct tee_client_device *tcdev)
> +{
> +	struct device *dev = &tcdev->dev;
> +	struct device_driver *driver = dev->driver;
> +
> +	driver->shutdown(dev);
> +}
> +
>  int __tee_client_driver_register(struct tee_client_driver *tee_driver,
>  				 struct module *owner)
>  {
>  	tee_driver->driver.owner = owner;
>  	tee_driver->driver.bus = &tee_bus_type;
>  
> +	/*
> +	 * Drivers that have callbacks set for tee_driver->driver need updating
> +	 * to use the callbacks in tee_driver instead. driver_register() warns
> +	 * about that, so no need to warn here, too.
> +	 */
> +	if (!tee_driver->probe && tee_driver->driver.probe)
> +		tee_driver->probe = tee_client_device_probe_legacy;
> +	if (!tee_driver->remove && tee_driver->driver.remove)
> +		tee_driver->remove = tee_client_device_remove_legacy;
> +	if (!tee_driver->shutdown && tee_driver->driver.probe)
> +		tee_driver->shutdown = tee_client_device_shutdown_legacy;
> +
>  	return driver_register(&tee_driver->driver);
>  }
>  EXPORT_SYMBOL_GPL(__tee_client_driver_register);
> diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h
> index 850c03b2cdea..e561a26f537a 100644
> --- a/include/linux/tee_drv.h
> +++ b/include/linux/tee_drv.h
> @@ -315,6 +315,9 @@ struct tee_client_device {
>   * @driver:		driver structure
>   */
>  struct tee_client_driver {
> +	int (*probe)(struct tee_client_device *);
> +	void (*remove)(struct tee_client_device *);
> +	void (*shutdown)(struct tee_client_device *);
>  	const struct tee_client_device_id *id_table;
>  	struct device_driver driver;
>  };
> -- 
> 2.47.3
> 
Re: [PATCH v2 02/17] tee: Add probe, remove and shutdown bus callbacks to tee_client_driver
Posted by Uwe Kleine-König 1 month, 3 weeks ago
Hello Sumit,

On Wed, Dec 17, 2025 at 02:24:55PM +0530, Sumit Garg wrote:
> On Mon, Dec 15, 2025 at 03:16:32PM +0100, Uwe Kleine-König wrote:
> > +static void tee_client_device_shutdown(struct device *dev)
> > +{
> > +	struct tee_client_device *tcdev = to_tee_client_device(dev);
> > +	struct tee_client_driver *drv = to_tee_client_driver(dev->driver);
> > +
> > +	if (dev->driver && drv->shutdown)
> 
> Is check for dev->driver really needed here? I think we only reach this
> stage if a driver is associated to the device. Also, it has been
> dereferenced above too.

Yes, this is needed. See device_shutdown() which calls the bus shutdown
method also for unbound drivers.

And no it has not been dereferenced above, to_tee_client_driver() is
just a subtraction from the literal value, this doesn't count as
dereference.

> Apart from this, rest looks fine to me. Feel free to add:
> 
> Reviewed-by: Sumit Garg <sumit.garg@oss.qualcomm.com>

Thanks
Uwe