tpm: fix reference counting for struct tpm_chip
[sfrench/cifs-2.6.git] / drivers / char / tpm / tpm-chip.c
index b009e7479b702f861997beed160b33be5a97d479..783d65fc71f07da60dc777a0a3cd4b76f3ad2366 100644 (file)
@@ -274,14 +274,6 @@ static void tpm_dev_release(struct device *dev)
        kfree(chip);
 }
 
-static void tpm_devs_release(struct device *dev)
-{
-       struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
-
-       /* release the master device reference */
-       put_device(&chip->dev);
-}
-
 /**
  * tpm_class_shutdown() - prepare the TPM device for loss of power.
  * @dev: device to which the chip is associated.
@@ -344,7 +336,6 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
        chip->dev_num = rc;
 
        device_initialize(&chip->dev);
-       device_initialize(&chip->devs);
 
        chip->dev.class = tpm_class;
        chip->dev.class->shutdown_pre = tpm_class_shutdown;
@@ -352,29 +343,12 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
        chip->dev.parent = pdev;
        chip->dev.groups = chip->groups;
 
-       chip->devs.parent = pdev;
-       chip->devs.class = tpmrm_class;
-       chip->devs.release = tpm_devs_release;
-       /* get extra reference on main device to hold on
-        * behalf of devs.  This holds the chip structure
-        * while cdevs is in use.  The corresponding put
-        * is in the tpm_devs_release (TPM2 only)
-        */
-       if (chip->flags & TPM_CHIP_FLAG_TPM2)
-               get_device(&chip->dev);
-
        if (chip->dev_num == 0)
                chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
        else
                chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
 
-       chip->devs.devt =
-               MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
-
        rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
-       if (rc)
-               goto out;
-       rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
        if (rc)
                goto out;
 
@@ -382,9 +356,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
                chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
 
        cdev_init(&chip->cdev, &tpm_fops);
-       cdev_init(&chip->cdevs, &tpmrm_fops);
        chip->cdev.owner = THIS_MODULE;
-       chip->cdevs.owner = THIS_MODULE;
 
        rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
        if (rc) {
@@ -396,7 +368,6 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
        return chip;
 
 out:
-       put_device(&chip->devs);
        put_device(&chip->dev);
        return ERR_PTR(rc);
 }
@@ -445,14 +416,9 @@ static int tpm_add_char_device(struct tpm_chip *chip)
        }
 
        if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
-               rc = cdev_device_add(&chip->cdevs, &chip->devs);
-               if (rc) {
-                       dev_err(&chip->devs,
-                               "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
-                               dev_name(&chip->devs), MAJOR(chip->devs.devt),
-                               MINOR(chip->devs.devt), rc);
-                       return rc;
-               }
+               rc = tpm_devs_add(chip);
+               if (rc)
+                       goto err_del_cdev;
        }
 
        /* Make the chip available. */
@@ -460,6 +426,10 @@ static int tpm_add_char_device(struct tpm_chip *chip)
        idr_replace(&dev_nums_idr, chip, chip->dev_num);
        mutex_unlock(&idr_lock);
 
+       return 0;
+
+err_del_cdev:
+       cdev_device_del(&chip->cdev, &chip->dev);
        return rc;
 }
 
@@ -654,7 +624,7 @@ void tpm_chip_unregister(struct tpm_chip *chip)
                hwrng_unregister(&chip->hwrng);
        tpm_bios_log_teardown(chip);
        if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
-               cdev_device_del(&chip->cdevs, &chip->devs);
+               tpm_devs_remove(chip);
        tpm_del_char_device(chip);
 }
 EXPORT_SYMBOL_GPL(tpm_chip_unregister);