tpm: rename chip->dev to chip->pdev
[cascardo/linux.git] / drivers / char / tpm / tpm_tis.c
index 2c46734..9e02489 100644 (file)
@@ -75,8 +75,9 @@ enum tis_defaults {
 #define        TPM_DID_VID(l)                  (0x0F00 | ((l) << 12))
 #define        TPM_RID(l)                      (0x0F04 | ((l) << 12))
 
-static LIST_HEAD(tis_chips);
-static DEFINE_MUTEX(tis_lock);
+struct priv_data {
+       bool irq_tested;
+};
 
 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
 static int is_itpm(struct pnp_dev *dev)
@@ -241,7 +242,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
        /* read first 10 bytes, including tag, paramsize, and result */
        if ((size =
             recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
-               dev_err(chip->dev, "Unable to read header\n");
+               dev_err(chip->pdev, "Unable to read header\n");
                goto out;
        }
 
@@ -254,7 +255,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
        if ((size +=
             recv_data(chip, &buf[TPM_HEADER_SIZE],
                       expected - TPM_HEADER_SIZE)) < expected) {
-               dev_err(chip->dev, "Unable to read remainder of result\n");
+               dev_err(chip->pdev, "Unable to read remainder of result\n");
                size = -ETIME;
                goto out;
        }
@@ -263,7 +264,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
                          &chip->vendor.int_queue, false);
        status = tpm_tis_status(chip);
        if (status & TPM_STS_DATA_AVAIL) {      /* retry? */
-               dev_err(chip->dev, "Error left over data\n");
+               dev_err(chip->pdev, "Error left over data\n");
                size = -EIO;
                goto out;
        }
@@ -338,12 +339,27 @@ out_err:
        return rc;
 }
 
+static void disable_interrupts(struct tpm_chip *chip)
+{
+       u32 intmask;
+
+       intmask =
+           ioread32(chip->vendor.iobase +
+                    TPM_INT_ENABLE(chip->vendor.locality));
+       intmask &= ~TPM_GLOBAL_INT_ENABLE;
+       iowrite32(intmask,
+                 chip->vendor.iobase +
+                 TPM_INT_ENABLE(chip->vendor.locality));
+       free_irq(chip->vendor.irq, chip);
+       chip->vendor.irq = 0;
+}
+
 /*
  * If interrupts are used (signaled by an irq set in the vendor structure)
  * tpm.c can skip polling for the data to be available as the interrupt is
  * waited for here
  */
-static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
+static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
 {
        int rc;
        u32 ordinal;
@@ -373,6 +389,30 @@ out_err:
        return rc;
 }
 
+static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
+{
+       int rc, irq;
+       struct priv_data *priv = chip->vendor.priv;
+
+       if (!chip->vendor.irq || priv->irq_tested)
+               return tpm_tis_send_main(chip, buf, len);
+
+       /* Verify receipt of the expected IRQ */
+       irq = chip->vendor.irq;
+       chip->vendor.irq = 0;
+       rc = tpm_tis_send_main(chip, buf, len);
+       chip->vendor.irq = irq;
+       if (!priv->irq_tested)
+               msleep(1);
+       if (!priv->irq_tested) {
+               disable_interrupts(chip);
+               dev_err(chip->pdev,
+                       FW_BUG "TPM interrupt not working, polling instead\n");
+       }
+       priv->irq_tested = true;
+       return rc;
+}
+
 struct tis_vendor_timeout_override {
        u32 did_vid;
        unsigned long timeout_us[4];
@@ -436,7 +476,7 @@ static int probe_itpm(struct tpm_chip *chip)
 
        rc = tpm_tis_send_data(chip, cmd_getticks, len);
        if (rc == 0) {
-               dev_info(chip->dev, "Detected an iTPM.\n");
+               dev_info(chip->pdev, "Detected an iTPM.\n");
                rc = 1;
        } else
                rc = -EFAULT;
@@ -505,6 +545,7 @@ static irqreturn_t tis_int_handler(int dummy, void *dev_id)
        if (interrupt == 0)
                return IRQ_NONE;
 
+       ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
        if (interrupt & TPM_INTF_DATA_AVAIL_INT)
                wake_up_interruptible(&chip->vendor.read_queue);
        if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
@@ -528,21 +569,40 @@ static bool interrupts = true;
 module_param(interrupts, bool, 0444);
 MODULE_PARM_DESC(interrupts, "Enable interrupts");
 
-static int tpm_tis_init(struct device *dev, resource_size_t start,
-                       resource_size_t len, unsigned int irq)
+static void tpm_tis_remove(struct tpm_chip *chip)
+{
+       iowrite32(~TPM_GLOBAL_INT_ENABLE &
+                 ioread32(chip->vendor.iobase +
+                          TPM_INT_ENABLE(chip->vendor.
+                                         locality)),
+                 chip->vendor.iobase +
+                 TPM_INT_ENABLE(chip->vendor.locality));
+       release_locality(chip, chip->vendor.locality, 1);
+}
+
+static int tpm_tis_init(struct device *dev, acpi_handle acpi_dev_handle,
+                       resource_size_t start, resource_size_t len,
+                       unsigned int irq)
 {
        u32 vendor, intfcaps, intmask;
        int rc, i, irq_s, irq_e, probe;
        struct tpm_chip *chip;
+       struct priv_data *priv;
 
-       if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
-               return -ENODEV;
+       priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
+       if (priv == NULL)
+               return -ENOMEM;
 
-       chip->vendor.iobase = ioremap(start, len);
-       if (!chip->vendor.iobase) {
-               rc = -EIO;
-               goto out_err;
-       }
+       chip = tpmm_chip_alloc(dev, &tpm_tis);
+       if (IS_ERR(chip))
+               return PTR_ERR(chip);
+
+       chip->vendor.priv = priv;
+       chip->acpi_dev_handle = acpi_dev_handle;
+
+       chip->vendor.iobase = devm_ioremap(dev, start, len);
+       if (!chip->vendor.iobase)
+               return -EIO;
 
        /* Default timeouts */
        chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
@@ -605,19 +665,6 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
        if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
                dev_dbg(dev, "\tData Avail Int Support\n");
 
-       /* get the timeouts before testing for irqs */
-       if (tpm_get_timeouts(chip)) {
-               dev_err(dev, "Could not get TPM timeouts and durations\n");
-               rc = -ENODEV;
-               goto out_err;
-       }
-
-       if (tpm_do_selftest(chip)) {
-               dev_err(dev, "TPM self test failed\n");
-               rc = -ENODEV;
-               goto out_err;
-       }
-
        /* INTERRUPT Setup */
        init_waitqueue_head(&chip->vendor.read_queue);
        init_waitqueue_head(&chip->vendor.int_queue);
@@ -649,10 +696,10 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
                for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
                        iowrite8(i, chip->vendor.iobase +
                                 TPM_INT_VECTOR(chip->vendor.locality));
-                       if (request_irq
-                           (i, tis_int_probe, IRQF_SHARED,
+                       if (devm_request_irq
+                           (dev, i, tis_int_probe, IRQF_SHARED,
                             chip->vendor.miscdev.name, chip) != 0) {
-                               dev_info(chip->dev,
+                               dev_info(chip->pdev,
                                         "Unable to request irq: %d for probe\n",
                                         i);
                                continue;
@@ -690,17 +737,16 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
                        iowrite32(intmask,
                                  chip->vendor.iobase +
                                  TPM_INT_ENABLE(chip->vendor.locality));
-                       free_irq(i, chip);
                }
        }
        if (chip->vendor.irq) {
                iowrite8(chip->vendor.irq,
                         chip->vendor.iobase +
                         TPM_INT_VECTOR(chip->vendor.locality));
-               if (request_irq
-                   (chip->vendor.irq, tis_int_handler, IRQF_SHARED,
+               if (devm_request_irq
+                   (dev, chip->vendor.irq, tis_int_handler, IRQF_SHARED,
                     chip->vendor.miscdev.name, chip) != 0) {
-                       dev_info(chip->dev,
+                       dev_info(chip->pdev,
                                 "Unable to request irq: %d for use\n",
                                 chip->vendor.irq);
                        chip->vendor.irq = 0;
@@ -719,17 +765,21 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
                }
        }
 
-       INIT_LIST_HEAD(&chip->vendor.list);
-       mutex_lock(&tis_lock);
-       list_add(&chip->vendor.list, &tis_chips);
-       mutex_unlock(&tis_lock);
+       if (tpm_get_timeouts(chip)) {
+               dev_err(dev, "Could not get TPM timeouts and durations\n");
+               rc = -ENODEV;
+               goto out_err;
+       }
 
+       if (tpm_do_selftest(chip)) {
+               dev_err(dev, "TPM self test failed\n");
+               rc = -ENODEV;
+               goto out_err;
+       }
 
-       return 0;
+       return tpm_chip_register(chip);
 out_err:
-       if (chip->vendor.iobase)
-               iounmap(chip->vendor.iobase);
-       tpm_remove_hardware(chip->dev);
+       tpm_tis_remove(chip);
        return rc;
 }
 
@@ -779,6 +829,7 @@ static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
 {
        resource_size_t start, len;
        unsigned int irq = 0;
+       acpi_handle acpi_dev_handle = NULL;
 
        start = pnp_mem_start(pnp_dev, 0);
        len = pnp_mem_len(pnp_dev, 0);
@@ -791,7 +842,10 @@ static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
        if (is_itpm(pnp_dev))
                itpm = true;
 
-       return tpm_tis_init(&pnp_dev->dev, start, len, irq);
+       if (pnp_acpi_device(pnp_dev))
+               acpi_dev_handle = pnp_acpi_device(pnp_dev)->handle;
+
+       return tpm_tis_init(&pnp_dev->dev, acpi_dev_handle, start, len, irq);
 }
 
 static struct pnp_device_id tpm_pnp_tbl[] = {
@@ -811,13 +865,10 @@ MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
 {
        struct tpm_chip *chip = pnp_get_drvdata(dev);
-
-       tpm_dev_vendor_release(chip);
-
-       kfree(chip);
+       tpm_chip_unregister(chip);
+       tpm_tis_remove(chip);
 }
 
-
 static struct pnp_driver tis_pnp_driver = {
        .name = "tpm_tis",
        .id_table = tpm_pnp_tbl,
@@ -836,8 +887,7 @@ MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
 
 static struct platform_driver tis_drv = {
        .driver = {
-               .name = "tpm_tis",
-               .owner          = THIS_MODULE,
+               .name           = "tpm_tis",
                .pm             = &tpm_tis_pm,
        },
 };
@@ -863,7 +913,7 @@ static int __init init_tis(void)
                rc = PTR_ERR(pdev);
                goto err_dev;
        }
-       rc = tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0);
+       rc = tpm_tis_init(&pdev->dev, NULL, TIS_MEM_BASE, TIS_MEM_LEN, 0);
        if (rc)
                goto err_init;
        return 0;
@@ -876,31 +926,16 @@ err_dev:
 
 static void __exit cleanup_tis(void)
 {
-       struct tpm_vendor_specific *i, *j;
        struct tpm_chip *chip;
-       mutex_lock(&tis_lock);
-       list_for_each_entry_safe(i, j, &tis_chips, list) {
-               chip = to_tpm_chip(i);
-               tpm_remove_hardware(chip->dev);
-               iowrite32(~TPM_GLOBAL_INT_ENABLE &
-                         ioread32(chip->vendor.iobase +
-                                  TPM_INT_ENABLE(chip->vendor.
-                                                 locality)),
-                         chip->vendor.iobase +
-                         TPM_INT_ENABLE(chip->vendor.locality));
-               release_locality(chip, chip->vendor.locality, 1);
-               if (chip->vendor.irq)
-                       free_irq(chip->vendor.irq, chip);
-               iounmap(i->iobase);
-               list_del(&i->list);
-       }
-       mutex_unlock(&tis_lock);
 #ifdef CONFIG_PNP
        if (!force) {
                pnp_unregister_driver(&tis_pnp_driver);
                return;
        }
 #endif
+       chip = dev_get_drvdata(&pdev->dev);
+       tpm_chip_unregister(chip);
+       tpm_tis_remove(chip);
        platform_device_unregister(pdev);
        platform_driver_unregister(&tis_drv);
 }