include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / char / tpm / tpm_nsc.c
index b412734..a605cb7 100644 (file)
@@ -7,7 +7,7 @@
  * Reiner Sailer <sailer@watson.ibm.com>
  * Kylene Hall <kjhall@us.ibm.com>
  *
- * Maintained by: <tpmdd_devel@lists.sourceforge.net>
+ * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  *
  * Device driver for TCG/TCPA TPM (trusted platform module).
  * Specifications at www.trustedcomputinggroup.org      
@@ -19,6 +19,8 @@
  * 
  */
 
+#include <linux/platform_device.h>
+#include <linux/slab.h>
 #include "tpm.h"
 
 /* National definitions */
@@ -70,7 +72,7 @@ static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
        unsigned long stop;
 
        /* status immediately available check */
-       *data = inb(chip->vendor->base + NSC_STATUS);
+       *data = inb(chip->vendor.base + NSC_STATUS);
        if ((*data & mask) == val)
                return 0;
 
@@ -78,7 +80,7 @@ static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
        stop = jiffies + 10 * HZ;
        do {
                msleep(TPM_TIMEOUT);
-               *data = inb(chip->vendor->base + 1);
+               *data = inb(chip->vendor.base + 1);
                if ((*data & mask) == val)
                        return 0;
        }
@@ -93,9 +95,9 @@ static int nsc_wait_for_ready(struct tpm_chip *chip)
        unsigned long stop;
 
        /* status immediately available check */
-       status = inb(chip->vendor->base + NSC_STATUS);
+       status = inb(chip->vendor.base + NSC_STATUS);
        if (status & NSC_STATUS_OBF)
-               status = inb(chip->vendor->base + NSC_DATA);
+               status = inb(chip->vendor.base + NSC_DATA);
        if (status & NSC_STATUS_RDY)
                return 0;
 
@@ -103,15 +105,15 @@ static int nsc_wait_for_ready(struct tpm_chip *chip)
        stop = jiffies + 100;
        do {
                msleep(TPM_TIMEOUT);
-               status = inb(chip->vendor->base + NSC_STATUS);
+               status = inb(chip->vendor.base + NSC_STATUS);
                if (status & NSC_STATUS_OBF)
-                       status = inb(chip->vendor->base + NSC_DATA);
+                       status = inb(chip->vendor.base + NSC_DATA);
                if (status & NSC_STATUS_RDY)
                        return 0;
        }
        while (time_before(jiffies, stop));
 
-       dev_info(&chip->pci_dev->dev, "wait for ready failed\n");
+       dev_info(chip->dev, "wait for ready failed\n");
        return -EBUSY;
 }
 
@@ -127,12 +129,12 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
                return -EIO;
 
        if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
-               dev_err(&chip->pci_dev->dev, "F0 timeout\n");
+               dev_err(chip->dev, "F0 timeout\n");
                return -EIO;
        }
        if ((data =
-            inb(chip->vendor->base + NSC_DATA)) != NSC_COMMAND_NORMAL) {
-               dev_err(&chip->pci_dev->dev, "not in normal mode (0x%x)\n",
+            inb(chip->vendor.base + NSC_DATA)) != NSC_COMMAND_NORMAL) {
+               dev_err(chip->dev, "not in normal mode (0x%x)\n",
                        data);
                return -EIO;
        }
@@ -141,22 +143,22 @@ static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
        for (p = buffer; p < &buffer[count]; p++) {
                if (wait_for_stat
                    (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
-                       dev_err(&chip->pci_dev->dev,
+                       dev_err(chip->dev,
                                "OBF timeout (while reading data)\n");
                        return -EIO;
                }
                if (data & NSC_STATUS_F0)
                        break;
-               *p = inb(chip->vendor->base + NSC_DATA);
+               *p = inb(chip->vendor.base + NSC_DATA);
        }
 
        if ((data & NSC_STATUS_F0) == 0 &&
        (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0)) {
-               dev_err(&chip->pci_dev->dev, "F0 not set\n");
+               dev_err(chip->dev, "F0 not set\n");
                return -EIO;
        }
-       if ((data = inb(chip->vendor->base + NSC_DATA)) != NSC_COMMAND_EOC) {
-               dev_err(&chip->pci_dev->dev,
+       if ((data = inb(chip->vendor.base + NSC_DATA)) != NSC_COMMAND_EOC) {
+               dev_err(chip->dev,
                        "expected end of command(0x%x)\n", data);
                return -EIO;
        }
@@ -181,46 +183,51 @@ static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
         * fix it. Not sure why this is needed, we followed the flow
         * chart in the manual to the letter.
         */
-       outb(NSC_COMMAND_CANCEL, chip->vendor->base + NSC_COMMAND);
+       outb(NSC_COMMAND_CANCEL, chip->vendor.base + NSC_COMMAND);
 
        if (nsc_wait_for_ready(chip) != 0)
                return -EIO;
 
        if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
-               dev_err(&chip->pci_dev->dev, "IBF timeout\n");
+               dev_err(chip->dev, "IBF timeout\n");
                return -EIO;
        }
 
-       outb(NSC_COMMAND_NORMAL, chip->vendor->base + NSC_COMMAND);
+       outb(NSC_COMMAND_NORMAL, chip->vendor.base + NSC_COMMAND);
        if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
-               dev_err(&chip->pci_dev->dev, "IBR timeout\n");
+               dev_err(chip->dev, "IBR timeout\n");
                return -EIO;
        }
 
        for (i = 0; i < count; i++) {
                if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
-                       dev_err(&chip->pci_dev->dev,
+                       dev_err(chip->dev,
                                "IBF timeout (while writing data)\n");
                        return -EIO;
                }
-               outb(buf[i], chip->vendor->base + NSC_DATA);
+               outb(buf[i], chip->vendor.base + NSC_DATA);
        }
 
        if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
-               dev_err(&chip->pci_dev->dev, "IBF timeout\n");
+               dev_err(chip->dev, "IBF timeout\n");
                return -EIO;
        }
-       outb(NSC_COMMAND_EOC, chip->vendor->base + NSC_COMMAND);
+       outb(NSC_COMMAND_EOC, chip->vendor.base + NSC_COMMAND);
 
        return count;
 }
 
 static void tpm_nsc_cancel(struct tpm_chip *chip)
 {
-       outb(NSC_COMMAND_CANCEL, chip->vendor->base + NSC_COMMAND);
+       outb(NSC_COMMAND_CANCEL, chip->vendor.base + NSC_COMMAND);
 }
 
-static struct file_operations nsc_ops = {
+static u8 tpm_nsc_status(struct tpm_chip *chip)
+{
+       return inb(chip->vendor.base + NSC_STATUS);
+}
+
+static const struct file_operations nsc_ops = {
        .owner = THIS_MODULE,
        .llseek = no_llseek,
        .open = tpm_open,
@@ -239,15 +246,16 @@ static struct attribute * nsc_attrs[] = {
        &dev_attr_pcrs.attr,
        &dev_attr_caps.attr,
        &dev_attr_cancel.attr,
-       0,
+       NULL,
 };
 
 static struct attribute_group nsc_attr_grp = { .attrs = nsc_attrs };
 
-static struct tpm_vendor_specific tpm_nsc = {
+static const struct tpm_vendor_specific tpm_nsc = {
        .recv = tpm_nsc_recv,
        .send = tpm_nsc_send,
        .cancel = tpm_nsc_cancel,
+       .status = tpm_nsc_status,
        .req_complete_mask = NSC_STATUS_OBF,
        .req_complete_val = NSC_STATUS_OBF,
        .req_canceled = NSC_STATUS_RDY,
@@ -255,55 +263,107 @@ static struct tpm_vendor_specific tpm_nsc = {
        .miscdev = { .fops = &nsc_ops, },
 };
 
-static int __devinit tpm_nsc_init(struct pci_dev *pci_dev,
-                                 const struct pci_device_id *pci_id)
+static struct platform_device *pdev = NULL;
+
+static void tpm_nsc_remove(struct device *dev)
 {
-       int rc = 0;
-       int lo, hi;
-       int nscAddrBase = TPM_ADDR;
+       struct tpm_chip *chip = dev_get_drvdata(dev);
+       if ( chip ) {
+               release_region(chip->vendor.base, 2);
+               tpm_remove_hardware(chip->dev);
+       }
+}
 
+static int tpm_nsc_suspend(struct platform_device *dev, pm_message_t msg)
+{
+       return tpm_pm_suspend(&dev->dev, msg);
+}
 
-       if (pci_enable_device(pci_dev))
-               return -EIO;
+static int tpm_nsc_resume(struct platform_device *dev)
+{
+       return tpm_pm_resume(&dev->dev);
+}
 
-       /* select PM channel 1 */
-       tpm_write_index(nscAddrBase,NSC_LDN_INDEX, 0x12);
+static struct platform_driver nsc_drv = {
+       .suspend         = tpm_nsc_suspend,
+       .resume          = tpm_nsc_resume,
+       .driver          = {
+               .name    = "tpm_nsc",
+               .owner   = THIS_MODULE,
+       },
+};
+
+static int __init init_nsc(void)
+{
+       int rc = 0;
+       int lo, hi, err;
+       int nscAddrBase = TPM_ADDR;
+       struct tpm_chip *chip;
+       unsigned long base;
 
        /* verify that it is a National part (SID) */
        if (tpm_read_index(TPM_ADDR, NSC_SID_INDEX) != 0xEF) {
                nscAddrBase = (tpm_read_index(TPM_SUPERIO_ADDR, 0x2C)<<8)|
                        (tpm_read_index(TPM_SUPERIO_ADDR, 0x2B)&0xFE);
-               if (tpm_read_index(nscAddrBase, NSC_SID_INDEX) != 0xF6) {
-                       rc = -ENODEV;
-                       goto out_err;
-               }
+               if (tpm_read_index(nscAddrBase, NSC_SID_INDEX) != 0xF6)
+                       return -ENODEV;
        }
 
+       err = platform_driver_register(&nsc_drv);
+       if (err)
+               return err;
+
        hi = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_HI);
        lo = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_LO);
-       tpm_nsc.base = (hi<<8) | lo;
+       base = (hi<<8) | lo;
+
+       /* enable the DPM module */
+       tpm_write_index(nscAddrBase, NSC_LDC_INDEX, 0x01);
+
+       pdev = platform_device_alloc("tpm_nscl0", -1);
+       if (!pdev) {
+               rc = -ENOMEM;
+               goto err_unreg_drv;
+       }
+
+       pdev->num_resources = 0;
+       pdev->dev.driver = &nsc_drv.driver;
+       pdev->dev.release = tpm_nsc_remove;
+
+       if ((rc = platform_device_register(pdev)) < 0)
+               goto err_free_dev;
+
+       if (request_region(base, 2, "tpm_nsc0") == NULL ) {
+               rc = -EBUSY;
+               goto err_unreg_dev;
+       }
+
+       if (!(chip = tpm_register_hardware(&pdev->dev, &tpm_nsc))) {
+               rc = -ENODEV;
+               goto err_rel_reg;
+       }
 
-       dev_dbg(&pci_dev->dev, "NSC TPM detected\n");
-       dev_dbg(&pci_dev->dev,
+       dev_dbg(&pdev->dev, "NSC TPM detected\n");
+       dev_dbg(&pdev->dev,
                "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
                tpm_read_index(nscAddrBase,0x07), tpm_read_index(nscAddrBase,0x20),
                tpm_read_index(nscAddrBase,0x27));
-       dev_dbg(&pci_dev->dev,
+       dev_dbg(&pdev->dev,
                "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
                tpm_read_index(nscAddrBase,0x21), tpm_read_index(nscAddrBase,0x25),
                tpm_read_index(nscAddrBase,0x26), tpm_read_index(nscAddrBase,0x28));
-       dev_dbg(&pci_dev->dev, "NSC IO Base0 0x%x\n",
+       dev_dbg(&pdev->dev, "NSC IO Base0 0x%x\n",
                (tpm_read_index(nscAddrBase,0x60) << 8) | tpm_read_index(nscAddrBase,0x61));
-       dev_dbg(&pci_dev->dev, "NSC IO Base1 0x%x\n",
+       dev_dbg(&pdev->dev, "NSC IO Base1 0x%x\n",
                (tpm_read_index(nscAddrBase,0x62) << 8) | tpm_read_index(nscAddrBase,0x63));
-       dev_dbg(&pci_dev->dev, "NSC Interrupt number and wakeup 0x%x\n",
+       dev_dbg(&pdev->dev, "NSC Interrupt number and wakeup 0x%x\n",
                tpm_read_index(nscAddrBase,0x70));
-       dev_dbg(&pci_dev->dev, "NSC IRQ type select 0x%x\n",
+       dev_dbg(&pdev->dev, "NSC IRQ type select 0x%x\n",
                tpm_read_index(nscAddrBase,0x71));
-       dev_dbg(&pci_dev->dev,
+       dev_dbg(&pdev->dev,
                "NSC DMA channel select0 0x%x, select1 0x%x\n",
                tpm_read_index(nscAddrBase,0x74), tpm_read_index(nscAddrBase,0x75));
-       dev_dbg(&pci_dev->dev,
+       dev_dbg(&pdev->dev,
                "NSC Config "
                "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
                tpm_read_index(nscAddrBase,0xF0), tpm_read_index(nscAddrBase,0xF1),
@@ -312,55 +372,35 @@ static int __devinit tpm_nsc_init(struct pci_dev *pci_dev,
                tpm_read_index(nscAddrBase,0xF6), tpm_read_index(nscAddrBase,0xF7),
                tpm_read_index(nscAddrBase,0xF8), tpm_read_index(nscAddrBase,0xF9));
 
-       dev_info(&pci_dev->dev,
+       dev_info(&pdev->dev,
                 "NSC TPM revision %d\n",
                 tpm_read_index(nscAddrBase, 0x27) & 0x1F);
 
-       /* enable the DPM module */
-       tpm_write_index(nscAddrBase, NSC_LDC_INDEX, 0x01);
-
-       if ((rc = tpm_register_hardware(pci_dev, &tpm_nsc)) < 0)
-               goto out_err;
+       chip->vendor.base = base;
 
        return 0;
 
-out_err:
-       pci_disable_device(pci_dev);
+err_rel_reg:
+       release_region(base, 2);
+err_unreg_dev:
+       platform_device_unregister(pdev);
+err_free_dev:
+       kfree(pdev);
+err_unreg_drv:
+       platform_driver_unregister(&nsc_drv);
        return rc;
 }
 
-static struct pci_device_id tpm_pci_tbl[] __devinitdata = {
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1)},
-       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0)},
-       {PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_LPC)},
-       {0,}
-};
-
-MODULE_DEVICE_TABLE(pci, tpm_pci_tbl);
-
-static struct pci_driver nsc_pci_driver = {
-       .name = "tpm_nsc",
-       .id_table = tpm_pci_tbl,
-       .probe = tpm_nsc_init,
-       .remove = __devexit_p(tpm_remove),
-       .suspend = tpm_pm_suspend,
-       .resume = tpm_pm_resume,
-};
-
-static int __init init_nsc(void)
-{
-       return pci_register_driver(&nsc_pci_driver);
-}
-
 static void __exit cleanup_nsc(void)
 {
-       pci_unregister_driver(&nsc_pci_driver);
+       if (pdev) {
+               tpm_nsc_remove(&pdev->dev);
+               platform_device_unregister(pdev);
+               kfree(pdev);
+               pdev = NULL;
+       }
+
+       platform_driver_unregister(&nsc_drv);
 }
 
 module_init(init_nsc);