aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/tpm
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-11-21 22:46:00 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-11-21 22:46:00 -0500
commit78dc53c422172a317adb0776dfb687057ffa28b7 (patch)
tree7c5d15da75d769d01f6a992c24c3490b3867d5b2 /drivers/char/tpm
parent3eaded86ac3e7f00fb3eeb8162d89e9a34e42fb0 (diff)
parent62fe318256befbd1b4a6765e71d9c997f768fe79 (diff)
Merge branch 'for-linus2' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull security subsystem updates from James Morris: "In this patchset, we finally get an SELinux update, with Paul Moore taking over as maintainer of that code. Also a significant update for the Keys subsystem, as well as maintenance updates to Smack, IMA, TPM, and Apparmor" and since I wanted to know more about the updates to key handling, here's the explanation from David Howells on that: "Okay. There are a number of separate bits. I'll go over the big bits and the odd important other bit, most of the smaller bits are just fixes and cleanups. If you want the small bits accounting for, I can do that too. (1) Keyring capacity expansion. KEYS: Consolidate the concept of an 'index key' for key access KEYS: Introduce a search context structure KEYS: Search for auth-key by name rather than target key ID Add a generic associative array implementation. KEYS: Expand the capacity of a keyring Several of the patches are providing an expansion of the capacity of a keyring. Currently, the maximum size of a keyring payload is one page. Subtract a small header and then divide up into pointers, that only gives you ~500 pointers on an x86_64 box. However, since the NFS idmapper uses a keyring to store ID mapping data, that has proven to be insufficient to the cause. Whatever data structure I use to handle the keyring payload, it can only store pointers to keys, not the keys themselves because several keyrings may point to a single key. This precludes inserting, say, and rb_node struct into the key struct for this purpose. I could make an rbtree of records such that each record has an rb_node and a key pointer, but that would use four words of space per key stored in the keyring. It would, however, be able to use much existing code. I selected instead a non-rebalancing radix-tree type approach as that could have a better space-used/key-pointer ratio. I could have used the radix tree implementation that we already have and insert keys into it by their serial numbers, but that means any sort of search must iterate over the whole radix tree. Further, its nodes are a bit on the capacious side for what I want - especially given that key serial numbers are randomly allocated, thus leaving a lot of empty space in the tree. So what I have is an associative array that internally is a radix-tree with 16 pointers per node where the index key is constructed from the key type pointer and the key description. This means that an exact lookup by type+description is very fast as this tells us how to navigate directly to the target key. I made the data structure general in lib/assoc_array.c as far as it is concerned, its index key is just a sequence of bits that leads to a pointer. It's possible that someone else will be able to make use of it also. FS-Cache might, for example. (2) Mark keys as 'trusted' and keyrings as 'trusted only'. KEYS: verify a certificate is signed by a 'trusted' key KEYS: Make the system 'trusted' keyring viewable by userspace KEYS: Add a 'trusted' flag and a 'trusted only' flag KEYS: Separate the kernel signature checking keyring from module signing These patches allow keys carrying asymmetric public keys to be marked as being 'trusted' and allow keyrings to be marked as only permitting the addition or linkage of trusted keys. Keys loaded from hardware during kernel boot or compiled into the kernel during build are marked as being trusted automatically. New keys can be loaded at runtime with add_key(). They are checked against the system keyring contents and if their signatures can be validated with keys that are already marked trusted, then they are marked trusted also and can thus be added into the master keyring. Patches from Mimi Zohar make this usable with the IMA keyrings also. (3) Remove the date checks on the key used to validate a module signature. X.509: Remove certificate date checks It's not reasonable to reject a signature just because the key that it was generated with is no longer valid datewise - especially if the kernel hasn't yet managed to set the system clock when the first module is loaded - so just remove those checks. (4) Make it simpler to deal with additional X.509 being loaded into the kernel. KEYS: Load *.x509 files into kernel keyring KEYS: Have make canonicalise the paths of the X.509 certs better to deduplicate The builder of the kernel now just places files with the extension ".x509" into the kernel source or build trees and they're concatenated by the kernel build and stuffed into the appropriate section. (5) Add support for userspace kerberos to use keyrings. KEYS: Add per-user_namespace registers for persistent per-UID kerberos caches KEYS: Implement a big key type that can save to tmpfs Fedora went to, by default, storing kerberos tickets and tokens in tmpfs. We looked at storing it in keyrings instead as that confers certain advantages such as tickets being automatically deleted after a certain amount of time and the ability for the kernel to get at these tokens more easily. To make this work, two things were needed: (a) A way for the tickets to persist beyond the lifetime of all a user's sessions so that cron-driven processes can still use them. The problem is that a user's session keyrings are deleted when the session that spawned them logs out and the user's user keyring is deleted when the UID is deleted (typically when the last log out happens), so neither of these places is suitable. I've added a system keyring into which a 'persistent' keyring is created for each UID on request. Each time a user requests their persistent keyring, the expiry time on it is set anew. If the user doesn't ask for it for, say, three days, the keyring is automatically expired and garbage collected using the existing gc. All the kerberos tokens it held are then also gc'd. (b) A key type that can hold really big tickets (up to 1MB in size). The problem is that Active Directory can return huge tickets with lots of auxiliary data attached. We don't, however, want to eat up huge tracts of unswappable kernel space for this, so if the ticket is greater than a certain size, we create a swappable shmem file and dump the contents in there and just live with the fact we then have an inode and a dentry overhead. If the ticket is smaller than that, we slap it in a kmalloc()'d buffer" * 'for-linus2' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (121 commits) KEYS: Fix keyring content gc scanner KEYS: Fix error handling in big_key instantiation KEYS: Fix UID check in keyctl_get_persistent() KEYS: The RSA public key algorithm needs to select MPILIB ima: define '_ima' as a builtin 'trusted' keyring ima: extend the measurement list to include the file signature kernel/system_certificate.S: use real contents instead of macro GLOBAL() KEYS: fix error return code in big_key_instantiate() KEYS: Fix keyring quota misaccounting on key replacement and unlink KEYS: Fix a race between negating a key and reading the error set KEYS: Make BIG_KEYS boolean apparmor: remove the "task" arg from may_change_ptraced_domain() apparmor: remove parent task info from audit logging apparmor: remove tsk field from the apparmor_audit_struct apparmor: fix capability to not use the current task, during reporting Smack: Ptrace access check mode ima: provide hash algo info in the xattr ima: enable support for larger default filedata hash algorithms ima: define kernel parameter 'ima_template=' to change configured default ima: add Kconfig default measurement list template ...
Diffstat (limited to 'drivers/char/tpm')
-rw-r--r--drivers/char/tpm/Kconfig37
-rw-r--r--drivers/char/tpm/Makefile11
-rw-r--r--drivers/char/tpm/tpm-interface.c (renamed from drivers/char/tpm/tpm.c)138
-rw-r--r--drivers/char/tpm/tpm.h3
-rw-r--r--drivers/char/tpm/tpm_atmel.c2
-rw-r--r--drivers/char/tpm/tpm_eventlog.c3
-rw-r--r--drivers/char/tpm/tpm_i2c_atmel.c284
-rw-r--r--drivers/char/tpm/tpm_i2c_infineon.c4
-rw-r--r--drivers/char/tpm/tpm_i2c_nuvoton.c710
-rw-r--r--drivers/char/tpm/tpm_i2c_stm_st33.c12
-rw-r--r--drivers/char/tpm/tpm_ibmvtpm.c6
-rw-r--r--drivers/char/tpm/tpm_ppi.c4
-rw-r--r--drivers/char/tpm/tpm_tis.c2
-rw-r--r--drivers/char/tpm/xen-tpmfront.c2
14 files changed, 1092 insertions, 126 deletions
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index 94c0c74434ea..1a65838888cd 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -33,6 +33,15 @@ config TCG_TIS
33 from within Linux. To compile this driver as a module, choose 33 from within Linux. To compile this driver as a module, choose
34 M here; the module will be called tpm_tis. 34 M here; the module will be called tpm_tis.
35 35
36config TCG_TIS_I2C_ATMEL
37 tristate "TPM Interface Specification 1.2 Interface (I2C - Atmel)"
38 depends on I2C
39 ---help---
40 If you have an Atmel I2C TPM security chip say Yes and it will be
41 accessible from within Linux.
42 To compile this driver as a module, choose M here; the module will
43 be called tpm_tis_i2c_atmel.
44
36config TCG_TIS_I2C_INFINEON 45config TCG_TIS_I2C_INFINEON
37 tristate "TPM Interface Specification 1.2 Interface (I2C - Infineon)" 46 tristate "TPM Interface Specification 1.2 Interface (I2C - Infineon)"
38 depends on I2C 47 depends on I2C
@@ -42,7 +51,17 @@ config TCG_TIS_I2C_INFINEON
42 Specification 0.20 say Yes and it will be accessible from within 51 Specification 0.20 say Yes and it will be accessible from within
43 Linux. 52 Linux.
44 To compile this driver as a module, choose M here; the module 53 To compile this driver as a module, choose M here; the module
45 will be called tpm_tis_i2c_infineon. 54 will be called tpm_i2c_infineon.
55
56config TCG_TIS_I2C_NUVOTON
57 tristate "TPM Interface Specification 1.2 Interface (I2C - Nuvoton)"
58 depends on I2C
59 ---help---
60 If you have a TPM security chip with an I2C interface from
61 Nuvoton Technology Corp. say Yes and it will be accessible
62 from within Linux.
63 To compile this driver as a module, choose M here; the module
64 will be called tpm_i2c_nuvoton.
46 65
47config TCG_NSC 66config TCG_NSC
48 tristate "National Semiconductor TPM Interface" 67 tristate "National Semiconductor TPM Interface"
@@ -82,14 +101,14 @@ config TCG_IBMVTPM
82 as a module, choose M here; the module will be called tpm_ibmvtpm. 101 as a module, choose M here; the module will be called tpm_ibmvtpm.
83 102
84config TCG_ST33_I2C 103config TCG_ST33_I2C
85 tristate "STMicroelectronics ST33 I2C TPM" 104 tristate "STMicroelectronics ST33 I2C TPM"
86 depends on I2C 105 depends on I2C
87 depends on GPIOLIB 106 depends on GPIOLIB
88 ---help--- 107 ---help---
89 If you have a TPM security chip from STMicroelectronics working with 108 If you have a TPM security chip from STMicroelectronics working with
90 an I2C bus say Yes and it will be accessible from within Linux. 109 an I2C bus say Yes and it will be accessible from within Linux.
91 To compile this driver as a module, choose M here; the module will be 110 To compile this driver as a module, choose M here; the module will be
92 called tpm_stm_st33_i2c. 111 called tpm_stm_st33_i2c.
93 112
94config TCG_XEN 113config TCG_XEN
95 tristate "XEN TPM Interface" 114 tristate "XEN TPM Interface"
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index eb41ff97d0ad..b80a4000daee 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -2,17 +2,20 @@
2# Makefile for the kernel tpm device drivers. 2# Makefile for the kernel tpm device drivers.
3# 3#
4obj-$(CONFIG_TCG_TPM) += tpm.o 4obj-$(CONFIG_TCG_TPM) += tpm.o
5tpm-y := tpm-interface.o
6tpm-$(CONFIG_ACPI) += tpm_ppi.o
7
5ifdef CONFIG_ACPI 8ifdef CONFIG_ACPI
6 obj-$(CONFIG_TCG_TPM) += tpm_bios.o 9 tpm-y += tpm_eventlog.o tpm_acpi.o
7 tpm_bios-objs += tpm_eventlog.o tpm_acpi.o tpm_ppi.o
8else 10else
9ifdef CONFIG_TCG_IBMVTPM 11ifdef CONFIG_TCG_IBMVTPM
10 obj-$(CONFIG_TCG_TPM) += tpm_bios.o 12 tpm-y += tpm_eventlog.o tpm_of.o
11 tpm_bios-objs += tpm_eventlog.o tpm_of.o
12endif 13endif
13endif 14endif
14obj-$(CONFIG_TCG_TIS) += tpm_tis.o 15obj-$(CONFIG_TCG_TIS) += tpm_tis.o
16obj-$(CONFIG_TCG_TIS_I2C_ATMEL) += tpm_i2c_atmel.o
15obj-$(CONFIG_TCG_TIS_I2C_INFINEON) += tpm_i2c_infineon.o 17obj-$(CONFIG_TCG_TIS_I2C_INFINEON) += tpm_i2c_infineon.o
18obj-$(CONFIG_TCG_TIS_I2C_NUVOTON) += tpm_i2c_nuvoton.o
16obj-$(CONFIG_TCG_NSC) += tpm_nsc.o 19obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
17obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o 20obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
18obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o 21obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm-interface.c
index e3c974a6c522..6ae41d337630 100644
--- a/drivers/char/tpm/tpm.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -10,13 +10,13 @@
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net> 10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * 11 *
12 * Device driver for TCG/TCPA TPM (trusted platform module). 12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org 13 * Specifications at www.trustedcomputinggroup.org
14 * 14 *
15 * This program is free software; you can redistribute it and/or 15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as 16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the 17 * published by the Free Software Foundation, version 2 of the
18 * License. 18 * License.
19 * 19 *
20 * Note, the TPM chip is not interrupt driven (only polling) 20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual 21 * and can have very long timeouts (minutes!). Hence the unusual
22 * calls to msleep. 22 * calls to msleep.
@@ -371,13 +371,14 @@ static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
371 return -ENODATA; 371 return -ENODATA;
372 if (count > bufsiz) { 372 if (count > bufsiz) {
373 dev_err(chip->dev, 373 dev_err(chip->dev,
374 "invalid count value %x %zx \n", count, bufsiz); 374 "invalid count value %x %zx\n", count, bufsiz);
375 return -E2BIG; 375 return -E2BIG;
376 } 376 }
377 377
378 mutex_lock(&chip->tpm_mutex); 378 mutex_lock(&chip->tpm_mutex);
379 379
380 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) { 380 rc = chip->vendor.send(chip, (u8 *) buf, count);
381 if (rc < 0) {
381 dev_err(chip->dev, 382 dev_err(chip->dev,
382 "tpm_transmit: tpm_send: error %zd\n", rc); 383 "tpm_transmit: tpm_send: error %zd\n", rc);
383 goto out; 384 goto out;
@@ -444,7 +445,7 @@ static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
444{ 445{
445 int err; 446 int err;
446 447
447 len = tpm_transmit(chip,(u8 *) cmd, len); 448 len = tpm_transmit(chip, (u8 *) cmd, len);
448 if (len < 0) 449 if (len < 0)
449 return len; 450 return len;
450 else if (len < TPM_HEADER_SIZE) 451 else if (len < TPM_HEADER_SIZE)
@@ -658,7 +659,7 @@ static int tpm_continue_selftest(struct tpm_chip *chip)
658 return rc; 659 return rc;
659} 660}
660 661
661ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr, 662ssize_t tpm_show_enabled(struct device *dev, struct device_attribute *attr,
662 char *buf) 663 char *buf)
663{ 664{
664 cap_t cap; 665 cap_t cap;
@@ -674,7 +675,7 @@ ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
674} 675}
675EXPORT_SYMBOL_GPL(tpm_show_enabled); 676EXPORT_SYMBOL_GPL(tpm_show_enabled);
676 677
677ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr, 678ssize_t tpm_show_active(struct device *dev, struct device_attribute *attr,
678 char *buf) 679 char *buf)
679{ 680{
680 cap_t cap; 681 cap_t cap;
@@ -690,7 +691,7 @@ ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
690} 691}
691EXPORT_SYMBOL_GPL(tpm_show_active); 692EXPORT_SYMBOL_GPL(tpm_show_active);
692 693
693ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr, 694ssize_t tpm_show_owned(struct device *dev, struct device_attribute *attr,
694 char *buf) 695 char *buf)
695{ 696{
696 cap_t cap; 697 cap_t cap;
@@ -706,8 +707,8 @@ ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
706} 707}
707EXPORT_SYMBOL_GPL(tpm_show_owned); 708EXPORT_SYMBOL_GPL(tpm_show_owned);
708 709
709ssize_t tpm_show_temp_deactivated(struct device * dev, 710ssize_t tpm_show_temp_deactivated(struct device *dev,
710 struct device_attribute * attr, char *buf) 711 struct device_attribute *attr, char *buf)
711{ 712{
712 cap_t cap; 713 cap_t cap;
713 ssize_t rc; 714 ssize_t rc;
@@ -769,10 +770,10 @@ static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
769 770
770/** 771/**
771 * tpm_pcr_read - read a pcr value 772 * tpm_pcr_read - read a pcr value
772 * @chip_num: tpm idx # or ANY 773 * @chip_num: tpm idx # or ANY
773 * @pcr_idx: pcr idx to retrieve 774 * @pcr_idx: pcr idx to retrieve
774 * @res_buf: TPM_PCR value 775 * @res_buf: TPM_PCR value
775 * size of res_buf is 20 bytes (or NULL if you don't care) 776 * size of res_buf is 20 bytes (or NULL if you don't care)
776 * 777 *
777 * The TPM driver should be built-in, but for whatever reason it 778 * The TPM driver should be built-in, but for whatever reason it
778 * isn't, protect against the chip disappearing, by incrementing 779 * isn't, protect against the chip disappearing, by incrementing
@@ -794,9 +795,9 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
794 795
795/** 796/**
796 * tpm_pcr_extend - extend pcr value with hash 797 * tpm_pcr_extend - extend pcr value with hash
797 * @chip_num: tpm idx # or AN& 798 * @chip_num: tpm idx # or AN&
798 * @pcr_idx: pcr idx to extend 799 * @pcr_idx: pcr idx to extend
799 * @hash: hash value used to extend pcr value 800 * @hash: hash value used to extend pcr value
800 * 801 *
801 * The TPM driver should be built-in, but for whatever reason it 802 * The TPM driver should be built-in, but for whatever reason it
802 * isn't, protect against the chip disappearing, by incrementing 803 * isn't, protect against the chip disappearing, by incrementing
@@ -847,8 +848,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
847 unsigned long duration; 848 unsigned long duration;
848 struct tpm_cmd_t cmd; 849 struct tpm_cmd_t cmd;
849 850
850 duration = tpm_calc_ordinal_duration(chip, 851 duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
851 TPM_ORD_CONTINUE_SELFTEST);
852 852
853 loops = jiffies_to_msecs(duration) / delay_msec; 853 loops = jiffies_to_msecs(duration) / delay_msec;
854 854
@@ -965,12 +965,12 @@ ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
965 if (err) 965 if (err)
966 goto out; 966 goto out;
967 967
968 /* 968 /*
969 ignore header 10 bytes 969 ignore header 10 bytes
970 algorithm 32 bits (1 == RSA ) 970 algorithm 32 bits (1 == RSA )
971 encscheme 16 bits 971 encscheme 16 bits
972 sigscheme 16 bits 972 sigscheme 16 bits
973 parameters (RSA 12->bytes: keybit, #primes, expbit) 973 parameters (RSA 12->bytes: keybit, #primes, expbit)
974 keylenbytes 32 bits 974 keylenbytes 32 bits
975 256 byte modulus 975 256 byte modulus
976 ignore checksum 20 bytes 976 ignore checksum 20 bytes
@@ -1020,43 +1020,33 @@ ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
1020 str += sprintf(str, "Manufacturer: 0x%x\n", 1020 str += sprintf(str, "Manufacturer: 0x%x\n",
1021 be32_to_cpu(cap.manufacturer_id)); 1021 be32_to_cpu(cap.manufacturer_id));
1022 1022
1023 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, 1023 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
1024 "attempting to determine the 1.1 version");
1025 if (rc)
1026 return 0;
1027 str += sprintf(str,
1028 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1029 cap.tpm_version.Major, cap.tpm_version.Minor,
1030 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1031 return str - buf;
1032}
1033EXPORT_SYMBOL_GPL(tpm_show_caps);
1034
1035ssize_t tpm_show_caps_1_2(struct device * dev,
1036 struct device_attribute * attr, char *buf)
1037{
1038 cap_t cap;
1039 ssize_t rc;
1040 char *str = buf;
1041
1042 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1043 "attempting to determine the manufacturer");
1044 if (rc)
1045 return 0;
1046 str += sprintf(str, "Manufacturer: 0x%x\n",
1047 be32_to_cpu(cap.manufacturer_id));
1048 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, 1024 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1049 "attempting to determine the 1.2 version"); 1025 "attempting to determine the 1.2 version");
1050 if (rc) 1026 if (!rc) {
1051 return 0; 1027 str += sprintf(str,
1052 str += sprintf(str, 1028 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1053 "TCG version: %d.%d\nFirmware version: %d.%d\n", 1029 cap.tpm_version_1_2.Major,
1054 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor, 1030 cap.tpm_version_1_2.Minor,
1055 cap.tpm_version_1_2.revMajor, 1031 cap.tpm_version_1_2.revMajor,
1056 cap.tpm_version_1_2.revMinor); 1032 cap.tpm_version_1_2.revMinor);
1033 } else {
1034 /* Otherwise just use TPM_STRUCT_VER */
1035 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
1036 "attempting to determine the 1.1 version");
1037 if (rc)
1038 return 0;
1039 str += sprintf(str,
1040 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1041 cap.tpm_version.Major,
1042 cap.tpm_version.Minor,
1043 cap.tpm_version.revMajor,
1044 cap.tpm_version.revMinor);
1045 }
1046
1057 return str - buf; 1047 return str - buf;
1058} 1048}
1059EXPORT_SYMBOL_GPL(tpm_show_caps_1_2); 1049EXPORT_SYMBOL_GPL(tpm_show_caps);
1060 1050
1061ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr, 1051ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1062 char *buf) 1052 char *buf)
@@ -1102,8 +1092,8 @@ ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1102} 1092}
1103EXPORT_SYMBOL_GPL(tpm_store_cancel); 1093EXPORT_SYMBOL_GPL(tpm_store_cancel);
1104 1094
1105static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, bool check_cancel, 1095static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
1106 bool *canceled) 1096 bool check_cancel, bool *canceled)
1107{ 1097{
1108 u8 status = chip->vendor.status(chip); 1098 u8 status = chip->vendor.status(chip);
1109 1099
@@ -1170,38 +1160,25 @@ EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1170 */ 1160 */
1171int tpm_open(struct inode *inode, struct file *file) 1161int tpm_open(struct inode *inode, struct file *file)
1172{ 1162{
1173 int minor = iminor(inode); 1163 struct miscdevice *misc = file->private_data;
1174 struct tpm_chip *chip = NULL, *pos; 1164 struct tpm_chip *chip = container_of(misc, struct tpm_chip,
1175 1165 vendor.miscdev);
1176 rcu_read_lock();
1177 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1178 if (pos->vendor.miscdev.minor == minor) {
1179 chip = pos;
1180 get_device(chip->dev);
1181 break;
1182 }
1183 }
1184 rcu_read_unlock();
1185
1186 if (!chip)
1187 return -ENODEV;
1188 1166
1189 if (test_and_set_bit(0, &chip->is_open)) { 1167 if (test_and_set_bit(0, &chip->is_open)) {
1190 dev_dbg(chip->dev, "Another process owns this TPM\n"); 1168 dev_dbg(chip->dev, "Another process owns this TPM\n");
1191 put_device(chip->dev);
1192 return -EBUSY; 1169 return -EBUSY;
1193 } 1170 }
1194 1171
1195 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 1172 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1196 if (chip->data_buffer == NULL) { 1173 if (chip->data_buffer == NULL) {
1197 clear_bit(0, &chip->is_open); 1174 clear_bit(0, &chip->is_open);
1198 put_device(chip->dev);
1199 return -ENOMEM; 1175 return -ENOMEM;
1200 } 1176 }
1201 1177
1202 atomic_set(&chip->data_pending, 0); 1178 atomic_set(&chip->data_pending, 0);
1203 1179
1204 file->private_data = chip; 1180 file->private_data = chip;
1181 get_device(chip->dev);
1205 return 0; 1182 return 0;
1206} 1183}
1207EXPORT_SYMBOL_GPL(tpm_open); 1184EXPORT_SYMBOL_GPL(tpm_open);
@@ -1463,7 +1440,6 @@ void tpm_dev_vendor_release(struct tpm_chip *chip)
1463 chip->vendor.release(chip->dev); 1440 chip->vendor.release(chip->dev);
1464 1441
1465 clear_bit(chip->dev_num, dev_mask); 1442 clear_bit(chip->dev_num, dev_mask);
1466 kfree(chip->vendor.miscdev.name);
1467} 1443}
1468EXPORT_SYMBOL_GPL(tpm_dev_vendor_release); 1444EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1469 1445
@@ -1487,7 +1463,7 @@ void tpm_dev_release(struct device *dev)
1487EXPORT_SYMBOL_GPL(tpm_dev_release); 1463EXPORT_SYMBOL_GPL(tpm_dev_release);
1488 1464
1489/* 1465/*
1490 * Called from tpm_<specific>.c probe function only for devices 1466 * Called from tpm_<specific>.c probe function only for devices
1491 * the driver has determined it should claim. Prior to calling 1467 * the driver has determined it should claim. Prior to calling
1492 * this function the specific probe function has called pci_enable_device 1468 * this function the specific probe function has called pci_enable_device
1493 * upon errant exit from this function specific probe function should call 1469 * upon errant exit from this function specific probe function should call
@@ -1496,17 +1472,13 @@ EXPORT_SYMBOL_GPL(tpm_dev_release);
1496struct tpm_chip *tpm_register_hardware(struct device *dev, 1472struct tpm_chip *tpm_register_hardware(struct device *dev,
1497 const struct tpm_vendor_specific *entry) 1473 const struct tpm_vendor_specific *entry)
1498{ 1474{
1499#define DEVNAME_SIZE 7
1500
1501 char *devname;
1502 struct tpm_chip *chip; 1475 struct tpm_chip *chip;
1503 1476
1504 /* Driver specific per-device data */ 1477 /* Driver specific per-device data */
1505 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 1478 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1506 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1507 1479
1508 if (chip == NULL || devname == NULL) 1480 if (chip == NULL)
1509 goto out_free; 1481 return NULL;
1510 1482
1511 mutex_init(&chip->buffer_mutex); 1483 mutex_init(&chip->buffer_mutex);
1512 mutex_init(&chip->tpm_mutex); 1484 mutex_init(&chip->tpm_mutex);
@@ -1531,8 +1503,9 @@ struct tpm_chip *tpm_register_hardware(struct device *dev,
1531 1503
1532 set_bit(chip->dev_num, dev_mask); 1504 set_bit(chip->dev_num, dev_mask);
1533 1505
1534 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num); 1506 scnprintf(chip->devname, sizeof(chip->devname), "%s%d", "tpm",
1535 chip->vendor.miscdev.name = devname; 1507 chip->dev_num);
1508 chip->vendor.miscdev.name = chip->devname;
1536 1509
1537 chip->vendor.miscdev.parent = dev; 1510 chip->vendor.miscdev.parent = dev;
1538 chip->dev = get_device(dev); 1511 chip->dev = get_device(dev);
@@ -1558,7 +1531,7 @@ struct tpm_chip *tpm_register_hardware(struct device *dev,
1558 goto put_device; 1531 goto put_device;
1559 } 1532 }
1560 1533
1561 chip->bios_dir = tpm_bios_log_setup(devname); 1534 chip->bios_dir = tpm_bios_log_setup(chip->devname);
1562 1535
1563 /* Make chip available */ 1536 /* Make chip available */
1564 spin_lock(&driver_lock); 1537 spin_lock(&driver_lock);
@@ -1571,7 +1544,6 @@ put_device:
1571 put_device(chip->dev); 1544 put_device(chip->dev);
1572out_free: 1545out_free:
1573 kfree(chip); 1546 kfree(chip);
1574 kfree(devname);
1575 return NULL; 1547 return NULL;
1576} 1548}
1577EXPORT_SYMBOL_GPL(tpm_register_hardware); 1549EXPORT_SYMBOL_GPL(tpm_register_hardware);
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index a7bfc176ed43..f32847872193 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -59,8 +59,6 @@ extern ssize_t tpm_show_pcrs(struct device *, struct device_attribute *attr,
59 char *); 59 char *);
60extern ssize_t tpm_show_caps(struct device *, struct device_attribute *attr, 60extern ssize_t tpm_show_caps(struct device *, struct device_attribute *attr,
61 char *); 61 char *);
62extern ssize_t tpm_show_caps_1_2(struct device *, struct device_attribute *attr,
63 char *);
64extern ssize_t tpm_store_cancel(struct device *, struct device_attribute *attr, 62extern ssize_t tpm_store_cancel(struct device *, struct device_attribute *attr,
65 const char *, size_t); 63 const char *, size_t);
66extern ssize_t tpm_show_enabled(struct device *, struct device_attribute *attr, 64extern ssize_t tpm_show_enabled(struct device *, struct device_attribute *attr,
@@ -122,6 +120,7 @@ struct tpm_chip {
122 struct device *dev; /* Device stuff */ 120 struct device *dev; /* Device stuff */
123 121
124 int dev_num; /* /dev/tpm# */ 122 int dev_num; /* /dev/tpm# */
123 char devname[7];
125 unsigned long is_open; /* only one allowed */ 124 unsigned long is_open; /* only one allowed */
126 int time_expired; 125 int time_expired;
127 126
diff --git a/drivers/char/tpm/tpm_atmel.c b/drivers/char/tpm/tpm_atmel.c
index 99d6820c611d..c9a528d25d22 100644
--- a/drivers/char/tpm/tpm_atmel.c
+++ b/drivers/char/tpm/tpm_atmel.c
@@ -202,7 +202,7 @@ static int __init init_atmel(void)
202 202
203 have_region = 203 have_region =
204 (atmel_request_region 204 (atmel_request_region
205 (tpm_atmel.base, region_size, "tpm_atmel0") == NULL) ? 0 : 1; 205 (base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
206 206
207 pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0); 207 pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
208 if (IS_ERR(pdev)) { 208 if (IS_ERR(pdev)) {
diff --git a/drivers/char/tpm/tpm_eventlog.c b/drivers/char/tpm/tpm_eventlog.c
index 84ddc557b8f8..59f7cb28260b 100644
--- a/drivers/char/tpm/tpm_eventlog.c
+++ b/drivers/char/tpm/tpm_eventlog.c
@@ -406,7 +406,6 @@ out_tpm:
406out: 406out:
407 return NULL; 407 return NULL;
408} 408}
409EXPORT_SYMBOL_GPL(tpm_bios_log_setup);
410 409
411void tpm_bios_log_teardown(struct dentry **lst) 410void tpm_bios_log_teardown(struct dentry **lst)
412{ 411{
@@ -415,5 +414,3 @@ void tpm_bios_log_teardown(struct dentry **lst)
415 for (i = 0; i < 3; i++) 414 for (i = 0; i < 3; i++)
416 securityfs_remove(lst[i]); 415 securityfs_remove(lst[i]);
417} 416}
418EXPORT_SYMBOL_GPL(tpm_bios_log_teardown);
419MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_i2c_atmel.c b/drivers/char/tpm/tpm_i2c_atmel.c
new file mode 100644
index 000000000000..c3cd7fe481a1
--- /dev/null
+++ b/drivers/char/tpm/tpm_i2c_atmel.c
@@ -0,0 +1,284 @@
1/*
2 * ATMEL I2C TPM AT97SC3204T
3 *
4 * Copyright (C) 2012 V Lab Technologies
5 * Teddy Reed <teddy@prosauce.org>
6 * Copyright (C) 2013, Obsidian Research Corp.
7 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
8 * Device driver for ATMEL I2C TPMs.
9 *
10 * Teddy Reed determined the basic I2C command flow, unlike other I2C TPM
11 * devices the raw TCG formatted TPM command data is written via I2C and then
12 * raw TCG formatted TPM command data is returned via I2C.
13 *
14 * TGC status/locality/etc functions seen in the LPC implementation do not
15 * seem to be present.
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see http://www.gnu.org/licenses/>.
29 */
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/slab.h>
34#include <linux/i2c.h>
35#include "tpm.h"
36
37#define I2C_DRIVER_NAME "tpm_i2c_atmel"
38
39#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
40#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
41
42#define ATMEL_STS_OK 1
43
44struct priv_data {
45 size_t len;
46 /* This is the amount we read on the first try. 25 was chosen to fit a
47 * fair number of read responses in the buffer so a 2nd retry can be
48 * avoided in small message cases. */
49 u8 buffer[sizeof(struct tpm_output_header) + 25];
50};
51
52static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
53{
54 struct priv_data *priv = chip->vendor.priv;
55 struct i2c_client *client = to_i2c_client(chip->dev);
56 s32 status;
57
58 priv->len = 0;
59
60 if (len <= 2)
61 return -EIO;
62
63 status = i2c_master_send(client, buf, len);
64
65 dev_dbg(chip->dev,
66 "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
67 (int)min_t(size_t, 64, len), buf, len, status);
68 return status;
69}
70
71static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
72{
73 struct priv_data *priv = chip->vendor.priv;
74 struct i2c_client *client = to_i2c_client(chip->dev);
75 struct tpm_output_header *hdr =
76 (struct tpm_output_header *)priv->buffer;
77 u32 expected_len;
78 int rc;
79
80 if (priv->len == 0)
81 return -EIO;
82
83 /* Get the message size from the message header, if we didn't get the
84 * whole message in read_status then we need to re-read the
85 * message. */
86 expected_len = be32_to_cpu(hdr->length);
87 if (expected_len > count)
88 return -ENOMEM;
89
90 if (priv->len >= expected_len) {
91 dev_dbg(chip->dev,
92 "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
93 (int)min_t(size_t, 64, expected_len), buf, count,
94 expected_len);
95 memcpy(buf, priv->buffer, expected_len);
96 return expected_len;
97 }
98
99 rc = i2c_master_recv(client, buf, expected_len);
100 dev_dbg(chip->dev,
101 "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
102 (int)min_t(size_t, 64, expected_len), buf, count,
103 expected_len);
104 return rc;
105}
106
107static void i2c_atmel_cancel(struct tpm_chip *chip)
108{
109 dev_err(chip->dev, "TPM operation cancellation was requested, but is not supported");
110}
111
112static u8 i2c_atmel_read_status(struct tpm_chip *chip)
113{
114 struct priv_data *priv = chip->vendor.priv;
115 struct i2c_client *client = to_i2c_client(chip->dev);
116 int rc;
117
118 /* The TPM fails the I2C read until it is ready, so we do the entire
119 * transfer here and buffer it locally. This way the common code can
120 * properly handle the timeouts. */
121 priv->len = 0;
122 memset(priv->buffer, 0, sizeof(priv->buffer));
123
124
125 /* Once the TPM has completed the command the command remains readable
126 * until another command is issued. */
127 rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
128 dev_dbg(chip->dev,
129 "%s: sts=%d", __func__, rc);
130 if (rc <= 0)
131 return 0;
132
133 priv->len = rc;
134
135 return ATMEL_STS_OK;
136}
137
138static const struct file_operations i2c_atmel_ops = {
139 .owner = THIS_MODULE,
140 .llseek = no_llseek,
141 .open = tpm_open,
142 .read = tpm_read,
143 .write = tpm_write,
144 .release = tpm_release,
145};
146
147static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
148static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
149static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
150static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
151static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
152static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
153static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
154static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
155static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
156static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
157
158static struct attribute *i2c_atmel_attrs[] = {
159 &dev_attr_pubek.attr,
160 &dev_attr_pcrs.attr,
161 &dev_attr_enabled.attr,
162 &dev_attr_active.attr,
163 &dev_attr_owned.attr,
164 &dev_attr_temp_deactivated.attr,
165 &dev_attr_caps.attr,
166 &dev_attr_cancel.attr,
167 &dev_attr_durations.attr,
168 &dev_attr_timeouts.attr,
169 NULL,
170};
171
172static struct attribute_group i2c_atmel_attr_grp = {
173 .attrs = i2c_atmel_attrs
174};
175
176static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
177{
178 return 0;
179}
180
181static const struct tpm_vendor_specific i2c_atmel = {
182 .status = i2c_atmel_read_status,
183 .recv = i2c_atmel_recv,
184 .send = i2c_atmel_send,
185 .cancel = i2c_atmel_cancel,
186 .req_complete_mask = ATMEL_STS_OK,
187 .req_complete_val = ATMEL_STS_OK,
188 .req_canceled = i2c_atmel_req_canceled,
189 .attr_group = &i2c_atmel_attr_grp,
190 .miscdev.fops = &i2c_atmel_ops,
191};
192
193static int i2c_atmel_probe(struct i2c_client *client,
194 const struct i2c_device_id *id)
195{
196 int rc;
197 struct tpm_chip *chip;
198 struct device *dev = &client->dev;
199
200 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
201 return -ENODEV;
202
203 chip = tpm_register_hardware(dev, &i2c_atmel);
204 if (!chip) {
205 dev_err(dev, "%s() error in tpm_register_hardware\n", __func__);
206 return -ENODEV;
207 }
208
209 chip->vendor.priv = devm_kzalloc(dev, sizeof(struct priv_data),
210 GFP_KERNEL);
211
212 /* Default timeouts */
213 chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
214 chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
215 chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
216 chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
217 chip->vendor.irq = 0;
218
219 /* There is no known way to probe for this device, and all version
220 * information seems to be read via TPM commands. Thus we rely on the
221 * TPM startup process in the common code to detect the device. */
222 if (tpm_get_timeouts(chip)) {
223 rc = -ENODEV;
224 goto out_err;
225 }
226
227 if (tpm_do_selftest(chip)) {
228 rc = -ENODEV;
229 goto out_err;
230 }
231
232 return 0;
233
234out_err:
235 tpm_dev_vendor_release(chip);
236 tpm_remove_hardware(chip->dev);
237 return rc;
238}
239
240static int i2c_atmel_remove(struct i2c_client *client)
241{
242 struct device *dev = &(client->dev);
243 struct tpm_chip *chip = dev_get_drvdata(dev);
244
245 if (chip)
246 tpm_dev_vendor_release(chip);
247 tpm_remove_hardware(dev);
248 kfree(chip);
249 return 0;
250}
251
252static const struct i2c_device_id i2c_atmel_id[] = {
253 {I2C_DRIVER_NAME, 0},
254 {}
255};
256MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
257
258#ifdef CONFIG_OF
259static const struct of_device_id i2c_atmel_of_match[] = {
260 {.compatible = "atmel,at97sc3204t"},
261 {},
262};
263MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
264#endif
265
266static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
267
268static struct i2c_driver i2c_atmel_driver = {
269 .id_table = i2c_atmel_id,
270 .probe = i2c_atmel_probe,
271 .remove = i2c_atmel_remove,
272 .driver = {
273 .name = I2C_DRIVER_NAME,
274 .owner = THIS_MODULE,
275 .pm = &i2c_atmel_pm_ops,
276 .of_match_table = of_match_ptr(i2c_atmel_of_match),
277 },
278};
279
280module_i2c_driver(i2c_atmel_driver);
281
282MODULE_AUTHOR("Jason Gunthorpe <jgunthorpe@obsidianresearch.com>");
283MODULE_DESCRIPTION("Atmel TPM I2C Driver");
284MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_i2c_infineon.c b/drivers/char/tpm/tpm_i2c_infineon.c
index b8735de8ce95..fefd2aa5c81e 100644
--- a/drivers/char/tpm/tpm_i2c_infineon.c
+++ b/drivers/char/tpm/tpm_i2c_infineon.c
@@ -581,7 +581,7 @@ static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
581static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); 581static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
582static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); 582static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
583static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL); 583static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
584static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); 584static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
585static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); 585static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
586static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); 586static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
587static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); 587static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
@@ -685,7 +685,6 @@ out_vendor:
685 chip->dev->release = NULL; 685 chip->dev->release = NULL;
686 chip->release = NULL; 686 chip->release = NULL;
687 tpm_dev.client = NULL; 687 tpm_dev.client = NULL;
688 dev_set_drvdata(chip->dev, chip);
689out_err: 688out_err:
690 return rc; 689 return rc;
691} 690}
@@ -766,7 +765,6 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
766 chip->dev->release = NULL; 765 chip->dev->release = NULL;
767 chip->release = NULL; 766 chip->release = NULL;
768 tpm_dev.client = NULL; 767 tpm_dev.client = NULL;
769 dev_set_drvdata(chip->dev, chip);
770 768
771 return 0; 769 return 0;
772} 770}
diff --git a/drivers/char/tpm/tpm_i2c_nuvoton.c b/drivers/char/tpm/tpm_i2c_nuvoton.c
new file mode 100644
index 000000000000..6276fea01ff0
--- /dev/null
+++ b/drivers/char/tpm/tpm_i2c_nuvoton.c
@@ -0,0 +1,710 @@
1/******************************************************************************
2 * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501,
3 * based on the TCG TPM Interface Spec version 1.2.
4 * Specifications at www.trustedcomputinggroup.org
5 *
6 * Copyright (C) 2011, Nuvoton Technology Corporation.
7 * Dan Morav <dan.morav@nuvoton.com>
8 * Copyright (C) 2013, Obsidian Research Corp.
9 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/>.
23 *
24 * Nuvoton contact information: APC.Support@nuvoton.com
25 *****************************************************************************/
26
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/wait.h>
33#include <linux/i2c.h>
34#include "tpm.h"
35
36/* I2C interface offsets */
37#define TPM_STS 0x00
38#define TPM_BURST_COUNT 0x01
39#define TPM_DATA_FIFO_W 0x20
40#define TPM_DATA_FIFO_R 0x40
41#define TPM_VID_DID_RID 0x60
42/* TPM command header size */
43#define TPM_HEADER_SIZE 10
44#define TPM_RETRY 5
45/*
46 * I2C bus device maximum buffer size w/o counting I2C address or command
47 * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
48 */
49#define TPM_I2C_MAX_BUF_SIZE 32
50#define TPM_I2C_RETRY_COUNT 32
51#define TPM_I2C_BUS_DELAY 1 /* msec */
52#define TPM_I2C_RETRY_DELAY_SHORT 2 /* msec */
53#define TPM_I2C_RETRY_DELAY_LONG 10 /* msec */
54
55#define I2C_DRIVER_NAME "tpm_i2c_nuvoton"
56
57struct priv_data {
58 unsigned int intrs;
59};
60
61static s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
62 u8 *data)
63{
64 s32 status;
65
66 status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
67 dev_dbg(&client->dev,
68 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
69 offset, size, (int)size, data, status);
70 return status;
71}
72
73static s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
74 u8 *data)
75{
76 s32 status;
77
78 status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
79 dev_dbg(&client->dev,
80 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
81 offset, size, (int)size, data, status);
82 return status;
83}
84
85#define TPM_STS_VALID 0x80
86#define TPM_STS_COMMAND_READY 0x40
87#define TPM_STS_GO 0x20
88#define TPM_STS_DATA_AVAIL 0x10
89#define TPM_STS_EXPECT 0x08
90#define TPM_STS_RESPONSE_RETRY 0x02
91#define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
92
93#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
94#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
95
96/* read TPM_STS register */
97static u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
98{
99 struct i2c_client *client = to_i2c_client(chip->dev);
100 s32 status;
101 u8 data;
102
103 status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
104 if (status <= 0) {
105 dev_err(chip->dev, "%s() error return %d\n", __func__,
106 status);
107 data = TPM_STS_ERR_VAL;
108 }
109
110 return data;
111}
112
113/* write byte to TPM_STS register */
114static s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
115{
116 s32 status;
117 int i;
118
119 /* this causes the current command to be aborted */
120 for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
121 status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
122 msleep(TPM_I2C_BUS_DELAY);
123 }
124 return status;
125}
126
127/* write commandReady to TPM_STS register */
128static void i2c_nuvoton_ready(struct tpm_chip *chip)
129{
130 struct i2c_client *client = to_i2c_client(chip->dev);
131 s32 status;
132
133 /* this causes the current command to be aborted */
134 status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
135 if (status < 0)
136 dev_err(chip->dev,
137 "%s() fail to write TPM_STS.commandReady\n", __func__);
138}
139
140/* read burstCount field from TPM_STS register
141 * return -1 on fail to read */
142static int i2c_nuvoton_get_burstcount(struct i2c_client *client,
143 struct tpm_chip *chip)
144{
145 unsigned long stop = jiffies + chip->vendor.timeout_d;
146 s32 status;
147 int burst_count = -1;
148 u8 data;
149
150 /* wait for burstcount to be non-zero */
151 do {
152 /* in I2C burstCount is 1 byte */
153 status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
154 &data);
155 if (status > 0 && data > 0) {
156 burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
157 break;
158 }
159 msleep(TPM_I2C_BUS_DELAY);
160 } while (time_before(jiffies, stop));
161
162 return burst_count;
163}
164
165/*
166 * WPCT301/NPCT501 SINT# supports only dataAvail
167 * any call to this function which is not waiting for dataAvail will
168 * set queue to NULL to avoid waiting for interrupt
169 */
170static bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
171{
172 u8 status = i2c_nuvoton_read_status(chip);
173 return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
174}
175
176static int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
177 u32 timeout, wait_queue_head_t *queue)
178{
179 if (chip->vendor.irq && queue) {
180 s32 rc;
181 DEFINE_WAIT(wait);
182 struct priv_data *priv = chip->vendor.priv;
183 unsigned int cur_intrs = priv->intrs;
184
185 enable_irq(chip->vendor.irq);
186 rc = wait_event_interruptible_timeout(*queue,
187 cur_intrs != priv->intrs,
188 timeout);
189 if (rc > 0)
190 return 0;
191 /* At this point we know that the SINT pin is asserted, so we
192 * do not need to do i2c_nuvoton_check_status */
193 } else {
194 unsigned long ten_msec, stop;
195 bool status_valid;
196
197 /* check current status */
198 status_valid = i2c_nuvoton_check_status(chip, mask, value);
199 if (status_valid)
200 return 0;
201
202 /* use polling to wait for the event */
203 ten_msec = jiffies + msecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
204 stop = jiffies + timeout;
205 do {
206 if (time_before(jiffies, ten_msec))
207 msleep(TPM_I2C_RETRY_DELAY_SHORT);
208 else
209 msleep(TPM_I2C_RETRY_DELAY_LONG);
210 status_valid = i2c_nuvoton_check_status(chip, mask,
211 value);
212 if (status_valid)
213 return 0;
214 } while (time_before(jiffies, stop));
215 }
216 dev_err(chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
217 value);
218 return -ETIMEDOUT;
219}
220
221/* wait for dataAvail field to be set in the TPM_STS register */
222static int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
223 wait_queue_head_t *queue)
224{
225 return i2c_nuvoton_wait_for_stat(chip,
226 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
227 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
228 timeout, queue);
229}
230
231/* Read @count bytes into @buf from TPM_RD_FIFO register */
232static int i2c_nuvoton_recv_data(struct i2c_client *client,
233 struct tpm_chip *chip, u8 *buf, size_t count)
234{
235 s32 rc;
236 int burst_count, bytes2read, size = 0;
237
238 while (size < count &&
239 i2c_nuvoton_wait_for_data_avail(chip,
240 chip->vendor.timeout_c,
241 &chip->vendor.read_queue) == 0) {
242 burst_count = i2c_nuvoton_get_burstcount(client, chip);
243 if (burst_count < 0) {
244 dev_err(chip->dev,
245 "%s() fail to read burstCount=%d\n", __func__,
246 burst_count);
247 return -EIO;
248 }
249 bytes2read = min_t(size_t, burst_count, count - size);
250 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
251 bytes2read, &buf[size]);
252 if (rc < 0) {
253 dev_err(chip->dev,
254 "%s() fail on i2c_nuvoton_read_buf()=%d\n",
255 __func__, rc);
256 return -EIO;
257 }
258 dev_dbg(chip->dev, "%s(%d):", __func__, bytes2read);
259 size += bytes2read;
260 }
261
262 return size;
263}
264
265/* Read TPM command results */
266static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
267{
268 struct device *dev = chip->dev;
269 struct i2c_client *client = to_i2c_client(dev);
270 s32 rc;
271 int expected, status, burst_count, retries, size = 0;
272
273 if (count < TPM_HEADER_SIZE) {
274 i2c_nuvoton_ready(chip); /* return to idle */
275 dev_err(dev, "%s() count < header size\n", __func__);
276 return -EIO;
277 }
278 for (retries = 0; retries < TPM_RETRY; retries++) {
279 if (retries > 0) {
280 /* if this is not the first trial, set responseRetry */
281 i2c_nuvoton_write_status(client,
282 TPM_STS_RESPONSE_RETRY);
283 }
284 /*
285 * read first available (> 10 bytes), including:
286 * tag, paramsize, and result
287 */
288 status = i2c_nuvoton_wait_for_data_avail(
289 chip, chip->vendor.timeout_c, &chip->vendor.read_queue);
290 if (status != 0) {
291 dev_err(dev, "%s() timeout on dataAvail\n", __func__);
292 size = -ETIMEDOUT;
293 continue;
294 }
295 burst_count = i2c_nuvoton_get_burstcount(client, chip);
296 if (burst_count < 0) {
297 dev_err(dev, "%s() fail to get burstCount\n", __func__);
298 size = -EIO;
299 continue;
300 }
301 size = i2c_nuvoton_recv_data(client, chip, buf,
302 burst_count);
303 if (size < TPM_HEADER_SIZE) {
304 dev_err(dev, "%s() fail to read header\n", __func__);
305 size = -EIO;
306 continue;
307 }
308 /*
309 * convert number of expected bytes field from big endian 32 bit
310 * to machine native
311 */
312 expected = be32_to_cpu(*(__be32 *) (buf + 2));
313 if (expected > count) {
314 dev_err(dev, "%s() expected > count\n", __func__);
315 size = -EIO;
316 continue;
317 }
318 rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
319 expected - size);
320 size += rc;
321 if (rc < 0 || size < expected) {
322 dev_err(dev, "%s() fail to read remainder of result\n",
323 __func__);
324 size = -EIO;
325 continue;
326 }
327 if (i2c_nuvoton_wait_for_stat(
328 chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
329 TPM_STS_VALID, chip->vendor.timeout_c,
330 NULL)) {
331 dev_err(dev, "%s() error left over data\n", __func__);
332 size = -ETIMEDOUT;
333 continue;
334 }
335 break;
336 }
337 i2c_nuvoton_ready(chip);
338 dev_dbg(chip->dev, "%s() -> %d\n", __func__, size);
339 return size;
340}
341
342/*
343 * Send TPM command.
344 *
345 * If interrupts are used (signaled by an irq set in the vendor structure)
346 * tpm.c can skip polling for the data to be available as the interrupt is
347 * waited for here
348 */
349static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
350{
351 struct device *dev = chip->dev;
352 struct i2c_client *client = to_i2c_client(dev);
353 u32 ordinal;
354 size_t count = 0;
355 int burst_count, bytes2write, retries, rc = -EIO;
356
357 for (retries = 0; retries < TPM_RETRY; retries++) {
358 i2c_nuvoton_ready(chip);
359 if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
360 TPM_STS_COMMAND_READY,
361 chip->vendor.timeout_b, NULL)) {
362 dev_err(dev, "%s() timeout on commandReady\n",
363 __func__);
364 rc = -EIO;
365 continue;
366 }
367 rc = 0;
368 while (count < len - 1) {
369 burst_count = i2c_nuvoton_get_burstcount(client,
370 chip);
371 if (burst_count < 0) {
372 dev_err(dev, "%s() fail get burstCount\n",
373 __func__);
374 rc = -EIO;
375 break;
376 }
377 bytes2write = min_t(size_t, burst_count,
378 len - 1 - count);
379 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
380 bytes2write, &buf[count]);
381 if (rc < 0) {
382 dev_err(dev, "%s() fail i2cWriteBuf\n",
383 __func__);
384 break;
385 }
386 dev_dbg(dev, "%s(%d):", __func__, bytes2write);
387 count += bytes2write;
388 rc = i2c_nuvoton_wait_for_stat(chip,
389 TPM_STS_VALID |
390 TPM_STS_EXPECT,
391 TPM_STS_VALID |
392 TPM_STS_EXPECT,
393 chip->vendor.timeout_c,
394 NULL);
395 if (rc < 0) {
396 dev_err(dev, "%s() timeout on Expect\n",
397 __func__);
398 rc = -ETIMEDOUT;
399 break;
400 }
401 }
402 if (rc < 0)
403 continue;
404
405 /* write last byte */
406 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
407 &buf[count]);
408 if (rc < 0) {
409 dev_err(dev, "%s() fail to write last byte\n",
410 __func__);
411 rc = -EIO;
412 continue;
413 }
414 dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
415 rc = i2c_nuvoton_wait_for_stat(chip,
416 TPM_STS_VALID | TPM_STS_EXPECT,
417 TPM_STS_VALID,
418 chip->vendor.timeout_c, NULL);
419 if (rc) {
420 dev_err(dev, "%s() timeout on Expect to clear\n",
421 __func__);
422 rc = -ETIMEDOUT;
423 continue;
424 }
425 break;
426 }
427 if (rc < 0) {
428 /* retries == TPM_RETRY */
429 i2c_nuvoton_ready(chip);
430 return rc;
431 }
432 /* execute the TPM command */
433 rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
434 if (rc < 0) {
435 dev_err(dev, "%s() fail to write Go\n", __func__);
436 i2c_nuvoton_ready(chip);
437 return rc;
438 }
439 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
440 rc = i2c_nuvoton_wait_for_data_avail(chip,
441 tpm_calc_ordinal_duration(chip,
442 ordinal),
443 &chip->vendor.read_queue);
444 if (rc) {
445 dev_err(dev, "%s() timeout command duration\n", __func__);
446 i2c_nuvoton_ready(chip);
447 return rc;
448 }
449
450 dev_dbg(dev, "%s() -> %zd\n", __func__, len);
451 return len;
452}
453
454static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
455{
456 return (status == TPM_STS_COMMAND_READY);
457}
458
459static const struct file_operations i2c_nuvoton_ops = {
460 .owner = THIS_MODULE,
461 .llseek = no_llseek,
462 .open = tpm_open,
463 .read = tpm_read,
464 .write = tpm_write,
465 .release = tpm_release,
466};
467
468static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
469static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
470static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
471static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
472static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
473static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
474static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
475static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
476static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
477static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
478
479static struct attribute *i2c_nuvoton_attrs[] = {
480 &dev_attr_pubek.attr,
481 &dev_attr_pcrs.attr,
482 &dev_attr_enabled.attr,
483 &dev_attr_active.attr,
484 &dev_attr_owned.attr,
485 &dev_attr_temp_deactivated.attr,
486 &dev_attr_caps.attr,
487 &dev_attr_cancel.attr,
488 &dev_attr_durations.attr,
489 &dev_attr_timeouts.attr,
490 NULL,
491};
492
493static struct attribute_group i2c_nuvoton_attr_grp = {
494 .attrs = i2c_nuvoton_attrs
495};
496
497static const struct tpm_vendor_specific tpm_i2c = {
498 .status = i2c_nuvoton_read_status,
499 .recv = i2c_nuvoton_recv,
500 .send = i2c_nuvoton_send,
501 .cancel = i2c_nuvoton_ready,
502 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
503 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
504 .req_canceled = i2c_nuvoton_req_canceled,
505 .attr_group = &i2c_nuvoton_attr_grp,
506 .miscdev.fops = &i2c_nuvoton_ops,
507};
508
509/* The only purpose for the handler is to signal to any waiting threads that
510 * the interrupt is currently being asserted. The driver does not do any
511 * processing triggered by interrupts, and the chip provides no way to mask at
512 * the source (plus that would be slow over I2C). Run the IRQ as a one-shot,
513 * this means it cannot be shared. */
514static irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
515{
516 struct tpm_chip *chip = dev_id;
517 struct priv_data *priv = chip->vendor.priv;
518
519 priv->intrs++;
520 wake_up(&chip->vendor.read_queue);
521 disable_irq_nosync(chip->vendor.irq);
522 return IRQ_HANDLED;
523}
524
525static int get_vid(struct i2c_client *client, u32 *res)
526{
527 static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
528 u32 temp;
529 s32 rc;
530
531 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
532 return -ENODEV;
533 rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
534 if (rc < 0)
535 return rc;
536
537 /* check WPCT301 values - ignore RID */
538 if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
539 /*
540 * f/w rev 2.81 has an issue where the VID_DID_RID is not
541 * reporting the right value. so give it another chance at
542 * offset 0x20 (FIFO_W).
543 */
544 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
545 (u8 *) (&temp));
546 if (rc < 0)
547 return rc;
548
549 /* check WPCT301 values - ignore RID */
550 if (memcmp(&temp, vid_did_rid_value,
551 sizeof(vid_did_rid_value)))
552 return -ENODEV;
553 }
554
555 *res = temp;
556 return 0;
557}
558
559static int i2c_nuvoton_probe(struct i2c_client *client,
560 const struct i2c_device_id *id)
561{
562 int rc;
563 struct tpm_chip *chip;
564 struct device *dev = &client->dev;
565 u32 vid = 0;
566
567 rc = get_vid(client, &vid);
568 if (rc)
569 return rc;
570
571 dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
572 (u8) (vid >> 16), (u8) (vid >> 24));
573
574 chip = tpm_register_hardware(dev, &tpm_i2c);
575 if (!chip) {
576 dev_err(dev, "%s() error in tpm_register_hardware\n", __func__);
577 return -ENODEV;
578 }
579
580 chip->vendor.priv = devm_kzalloc(dev, sizeof(struct priv_data),
581 GFP_KERNEL);
582 init_waitqueue_head(&chip->vendor.read_queue);
583 init_waitqueue_head(&chip->vendor.int_queue);
584
585 /* Default timeouts */
586 chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
587 chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
588 chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
589 chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
590
591 /*
592 * I2C intfcaps (interrupt capabilitieis) in the chip are hard coded to:
593 * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
594 * The IRQ should be set in the i2c_board_info (which is done
595 * automatically in of_i2c_register_devices, for device tree users */
596 chip->vendor.irq = client->irq;
597
598 if (chip->vendor.irq) {
599 dev_dbg(dev, "%s() chip-vendor.irq\n", __func__);
600 rc = devm_request_irq(dev, chip->vendor.irq,
601 i2c_nuvoton_int_handler,
602 IRQF_TRIGGER_LOW,
603 chip->vendor.miscdev.name,
604 chip);
605 if (rc) {
606 dev_err(dev, "%s() Unable to request irq: %d for use\n",
607 __func__, chip->vendor.irq);
608 chip->vendor.irq = 0;
609 } else {
610 /* Clear any pending interrupt */
611 i2c_nuvoton_ready(chip);
612 /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
613 rc = i2c_nuvoton_wait_for_stat(chip,
614 TPM_STS_COMMAND_READY,
615 TPM_STS_COMMAND_READY,
616 chip->vendor.timeout_b,
617 NULL);
618 if (rc == 0) {
619 /*
620 * TIS is in ready state
621 * write dummy byte to enter reception state
622 * TPM_DATA_FIFO_W <- rc (0)
623 */
624 rc = i2c_nuvoton_write_buf(client,
625 TPM_DATA_FIFO_W,
626 1, (u8 *) (&rc));
627 if (rc < 0)
628 goto out_err;
629 /* TPM_STS <- 0x40 (commandReady) */
630 i2c_nuvoton_ready(chip);
631 } else {
632 /*
633 * timeout_b reached - command was
634 * aborted. TIS should now be in idle state -
635 * only TPM_STS_VALID should be set
636 */
637 if (i2c_nuvoton_read_status(chip) !=
638 TPM_STS_VALID) {
639 rc = -EIO;
640 goto out_err;
641 }
642 }
643 }
644 }
645
646 if (tpm_get_timeouts(chip)) {
647 rc = -ENODEV;
648 goto out_err;
649 }
650
651 if (tpm_do_selftest(chip)) {
652 rc = -ENODEV;
653 goto out_err;
654 }
655
656 return 0;
657
658out_err:
659 tpm_dev_vendor_release(chip);
660 tpm_remove_hardware(chip->dev);
661 return rc;
662}
663
664static int i2c_nuvoton_remove(struct i2c_client *client)
665{
666 struct device *dev = &(client->dev);
667 struct tpm_chip *chip = dev_get_drvdata(dev);
668
669 if (chip)
670 tpm_dev_vendor_release(chip);
671 tpm_remove_hardware(dev);
672 kfree(chip);
673 return 0;
674}
675
676
677static const struct i2c_device_id i2c_nuvoton_id[] = {
678 {I2C_DRIVER_NAME, 0},
679 {}
680};
681MODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
682
683#ifdef CONFIG_OF
684static const struct of_device_id i2c_nuvoton_of_match[] = {
685 {.compatible = "nuvoton,npct501"},
686 {.compatible = "winbond,wpct301"},
687 {},
688};
689MODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
690#endif
691
692static SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
693
694static struct i2c_driver i2c_nuvoton_driver = {
695 .id_table = i2c_nuvoton_id,
696 .probe = i2c_nuvoton_probe,
697 .remove = i2c_nuvoton_remove,
698 .driver = {
699 .name = I2C_DRIVER_NAME,
700 .owner = THIS_MODULE,
701 .pm = &i2c_nuvoton_pm_ops,
702 .of_match_table = of_match_ptr(i2c_nuvoton_of_match),
703 },
704};
705
706module_i2c_driver(i2c_nuvoton_driver);
707
708MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
709MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
710MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_i2c_stm_st33.c b/drivers/char/tpm/tpm_i2c_stm_st33.c
index 5bb8e2ddd3b3..a0d6ceb5d005 100644
--- a/drivers/char/tpm/tpm_i2c_stm_st33.c
+++ b/drivers/char/tpm/tpm_i2c_stm_st33.c
@@ -584,7 +584,7 @@ static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
584static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); 584static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
585static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); 585static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
586static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL); 586static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
587static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); 587static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
588static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); 588static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
589 589
590static struct attribute *stm_tpm_attrs[] = { 590static struct attribute *stm_tpm_attrs[] = {
@@ -746,8 +746,6 @@ tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
746 746
747 tpm_get_timeouts(chip); 747 tpm_get_timeouts(chip);
748 748
749 i2c_set_clientdata(client, chip);
750
751 dev_info(chip->dev, "TPM I2C Initialized\n"); 749 dev_info(chip->dev, "TPM I2C Initialized\n");
752 return 0; 750 return 0;
753_irq_set: 751_irq_set:
@@ -807,24 +805,18 @@ static int tpm_st33_i2c_remove(struct i2c_client *client)
807#ifdef CONFIG_PM_SLEEP 805#ifdef CONFIG_PM_SLEEP
808/* 806/*
809 * tpm_st33_i2c_pm_suspend suspend the TPM device 807 * tpm_st33_i2c_pm_suspend suspend the TPM device
810 * Added: Work around when suspend and no tpm application is running, suspend
811 * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
812 * TPM core)
813 * @param: client, the i2c_client drescription (TPM I2C description). 808 * @param: client, the i2c_client drescription (TPM I2C description).
814 * @param: mesg, the power management message. 809 * @param: mesg, the power management message.
815 * @return: 0 in case of success. 810 * @return: 0 in case of success.
816 */ 811 */
817static int tpm_st33_i2c_pm_suspend(struct device *dev) 812static int tpm_st33_i2c_pm_suspend(struct device *dev)
818{ 813{
819 struct tpm_chip *chip = dev_get_drvdata(dev);
820 struct st33zp24_platform_data *pin_infos = dev->platform_data; 814 struct st33zp24_platform_data *pin_infos = dev->platform_data;
821 int ret = 0; 815 int ret = 0;
822 816
823 if (power_mgt) { 817 if (power_mgt) {
824 gpio_set_value(pin_infos->io_lpcpd, 0); 818 gpio_set_value(pin_infos->io_lpcpd, 0);
825 } else { 819 } else {
826 if (chip->data_buffer == NULL)
827 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
828 ret = tpm_pm_suspend(dev); 820 ret = tpm_pm_suspend(dev);
829 } 821 }
830 return ret; 822 return ret;
@@ -849,8 +841,6 @@ static int tpm_st33_i2c_pm_resume(struct device *dev)
849 TPM_STS_VALID) == TPM_STS_VALID, 841 TPM_STS_VALID) == TPM_STS_VALID,
850 chip->vendor.timeout_b); 842 chip->vendor.timeout_b);
851 } else { 843 } else {
852 if (chip->data_buffer == NULL)
853 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
854 ret = tpm_pm_resume(dev); 844 ret = tpm_pm_resume(dev);
855 if (!ret) 845 if (!ret)
856 tpm_do_selftest(chip); 846 tpm_do_selftest(chip);
diff --git a/drivers/char/tpm/tpm_ibmvtpm.c b/drivers/char/tpm/tpm_ibmvtpm.c
index 56b07c35a13e..2783a42aa732 100644
--- a/drivers/char/tpm/tpm_ibmvtpm.c
+++ b/drivers/char/tpm/tpm_ibmvtpm.c
@@ -98,7 +98,7 @@ static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
98 98
99 if (count < len) { 99 if (count < len) {
100 dev_err(ibmvtpm->dev, 100 dev_err(ibmvtpm->dev,
101 "Invalid size in recv: count=%ld, crq_size=%d\n", 101 "Invalid size in recv: count=%zd, crq_size=%d\n",
102 count, len); 102 count, len);
103 return -EIO; 103 return -EIO;
104 } 104 }
@@ -136,7 +136,7 @@ static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
136 136
137 if (count > ibmvtpm->rtce_size) { 137 if (count > ibmvtpm->rtce_size) {
138 dev_err(ibmvtpm->dev, 138 dev_err(ibmvtpm->dev,
139 "Invalid size in send: count=%ld, rtce_size=%d\n", 139 "Invalid size in send: count=%zd, rtce_size=%d\n",
140 count, ibmvtpm->rtce_size); 140 count, ibmvtpm->rtce_size);
141 return -EIO; 141 return -EIO;
142 } 142 }
@@ -419,7 +419,7 @@ static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
419static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); 419static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
420static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, 420static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
421 NULL); 421 NULL);
422static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); 422static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
423static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); 423static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
424static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); 424static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
425static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); 425static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
diff --git a/drivers/char/tpm/tpm_ppi.c b/drivers/char/tpm/tpm_ppi.c
index 2168d15bc728..8e562dc65601 100644
--- a/drivers/char/tpm/tpm_ppi.c
+++ b/drivers/char/tpm/tpm_ppi.c
@@ -452,12 +452,8 @@ int tpm_add_ppi(struct kobject *parent)
452{ 452{
453 return sysfs_create_group(parent, &ppi_attr_grp); 453 return sysfs_create_group(parent, &ppi_attr_grp);
454} 454}
455EXPORT_SYMBOL_GPL(tpm_add_ppi);
456 455
457void tpm_remove_ppi(struct kobject *parent) 456void tpm_remove_ppi(struct kobject *parent)
458{ 457{
459 sysfs_remove_group(parent, &ppi_attr_grp); 458 sysfs_remove_group(parent, &ppi_attr_grp);
460} 459}
461EXPORT_SYMBOL_GPL(tpm_remove_ppi);
462
463MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_tis.c b/drivers/char/tpm/tpm_tis.c
index 5796d0157ce0..1b74459c0723 100644
--- a/drivers/char/tpm/tpm_tis.c
+++ b/drivers/char/tpm/tpm_tis.c
@@ -448,7 +448,7 @@ static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
448static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); 448static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
449static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, 449static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
450 NULL); 450 NULL);
451static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); 451static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
452static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); 452static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
453static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); 453static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
454static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); 454static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
diff --git a/drivers/char/tpm/xen-tpmfront.c b/drivers/char/tpm/xen-tpmfront.c
index 94c280d36e8b..c8ff4df81779 100644
--- a/drivers/char/tpm/xen-tpmfront.c
+++ b/drivers/char/tpm/xen-tpmfront.c
@@ -351,8 +351,6 @@ static int tpmfront_probe(struct xenbus_device *dev,
351 351
352 tpm_get_timeouts(priv->chip); 352 tpm_get_timeouts(priv->chip);
353 353
354 dev_set_drvdata(&dev->dev, priv->chip);
355
356 return rv; 354 return rv;
357} 355}
358 356