Patchwork [03/23] TPM: Provide a platform driver for the user emulator driver

login
register
mail settings
Submitter David Howells
Date Aug. 21, 2018, 3:57 p.m.
Message ID <153486702979.13066.16900998092976336647.stgit@warthog.procyon.org.uk>
Download mbox | patch
Permalink /patch/597741/
State New
Headers show

Comments

David Howells - Aug. 21, 2018, 3:57 p.m.
Provide a platform driver for the user emulator driver.  This seems to be
necessary to stop tpm_chip_find_get() from blowing up because it assumes
unconditionally that any device will have a driver attached:

		if (try_module_get(pos->dev->driver->owner)) {

However, this doesn't then work right because if I remove the TPM device and
re-add it, the tpm ID isn't recycled (ie, /dev/tpm0 becomes unavailable and
the new TPM is /dev/tpm1).

Signed-off-by: David Howells <dhowells@redhat.com>
---

 drivers/char/tpm/tpm_user_emul.c |   24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Jarkko Sakkinen - Aug. 24, 2018, 6:30 a.m.
On Tue, Aug 21, 2018 at 04:57:09PM +0100, David Howells wrote:
> Provide a platform driver for the user emulator driver.  This seems to be
> necessary to stop tpm_chip_find_get() from blowing up because it assumes
> unconditionally that any device will have a driver attached:
> 
> 		if (try_module_get(pos->dev->driver->owner)) {
> 
> However, this doesn't then work right because if I remove the TPM device and
> re-add it, the tpm ID isn't recycled (ie, /dev/tpm0 becomes unavailable and
> the new TPM is /dev/tpm1).
> 
> Signed-off-by: David Howells <dhowells@redhat.com>

Again, a duplicate.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

Patch

diff --git a/drivers/char/tpm/tpm_user_emul.c b/drivers/char/tpm/tpm_user_emul.c
index b96350592bca..e5b13358c71e 100644
--- a/drivers/char/tpm/tpm_user_emul.c
+++ b/drivers/char/tpm/tpm_user_emul.c
@@ -656,17 +656,39 @@  static struct miscdevice tpm_user_dev = {
 	.fops	= &tpm_user_fops,
 };
 
+static struct platform_driver tpm_user_drv = {
+	.driver = {
+		.name	= "tpm_user",
+		.owner	= THIS_MODULE,
+		/* .pm	= &tpm_user_pm, -- do we need pm since there's no h/w? */
+	},
+};
+
 /*
  * Initialise a device
  */
 static __init int tpm_user_mod_init(void)
 {
-	return misc_register(&tpm_user_dev);
+	int ret;
+
+	ret = platform_driver_register(&tpm_user_drv);
+	if (ret < 0)
+		return ret;
+
+	ret = misc_register(&tpm_user_dev);
+	if (ret < 0)
+		goto error_dev;
+	return 0;
+
+error_dev:
+	platform_driver_unregister(&tpm_user_drv);
+	return ret;
 }
 device_initcall(tpm_user_mod_init);
 
 static __exit void tpm_user_mod_exit(void)
 {
 	misc_deregister(&tpm_user_dev);
+	platform_driver_unregister(&tpm_user_drv);
 }
 module_exit(tpm_user_mod_exit);