2 * Copyright (C) 2004 IBM Corporation
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
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
17 * published by the Free Software Foundation, version 2 of the
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
26 #include <linux/poll.h>
27 #include <linux/slab.h>
28 #include <linux/mutex.h>
29 #include <linux/spinlock.h>
34 TPM_MINOR = 224, /* officially assigned */
36 TPM_NUM_DEVICES = 256,
46 #define TPM_MAX_ORDINAL 243
47 #define TPM_MAX_PROTECTED_ORDINAL 12
48 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
51 * Bug workaround - some TPM's don't flush the most
52 * recently changed pcr on suspend, so force the flush
53 * with an extend to the selected _unused_ non-volatile pcr.
55 static int tpm_suspend_pcr;
56 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
57 MODULE_PARM_DESC(suspend_pcr,
58 "PCR to use for dummy writes to faciltate flush on suspend.");
60 static LIST_HEAD(tpm_chip_list);
61 static DEFINE_SPINLOCK(driver_lock);
62 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
65 * Array with one entry per ordinal defining the maximum amount
66 * of time the chip could take to return the result. The ordinal
67 * designation of short, medium or long is defined in a table in
68 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
69 * values of the SHORT, MEDIUM, and LONG durations are retrieved
70 * from the chip during initialization with a call to tpm_get_timeouts.
72 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
73 TPM_UNDEFINED, /* 0 */
78 TPM_UNDEFINED, /* 5 */
87 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
88 TPM_UNDEFINED, /* 0 */
93 TPM_UNDEFINED, /* 5 */
143 TPM_UNDEFINED, /* 55 */
163 TPM_UNDEFINED, /* 75 */
173 TPM_UNDEFINED, /* 85 */
183 TPM_UNDEFINED, /* 95 */
188 TPM_MEDIUM, /* 100 */
193 TPM_UNDEFINED, /* 105 */
223 TPM_UNDEFINED, /* 135 */
233 TPM_UNDEFINED, /* 145 */
243 TPM_UNDEFINED, /* 155 */
253 TPM_UNDEFINED, /* 165 */
263 TPM_UNDEFINED, /* 175 */
268 TPM_MEDIUM, /* 180 */
273 TPM_MEDIUM, /* 185 */
278 TPM_UNDEFINED, /* 190 */
283 TPM_UNDEFINED, /* 195 */
298 TPM_MEDIUM, /* 210 */
303 TPM_UNDEFINED, /* 215 */
313 TPM_UNDEFINED, /* 225 */
323 TPM_UNDEFINED, /* 235 */
333 static void user_reader_timeout(unsigned long ptr)
335 struct tpm_chip *chip = (struct tpm_chip *) ptr;
337 schedule_work(&chip->work);
340 static void timeout_work(struct work_struct *work)
342 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
344 mutex_lock(&chip->buffer_mutex);
345 atomic_set(&chip->data_pending, 0);
346 memset(chip->data_buffer, 0, TPM_BUFSIZE);
347 mutex_unlock(&chip->buffer_mutex);
351 * Returns max number of jiffies to wait
353 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
356 int duration_idx = TPM_UNDEFINED;
359 if (ordinal < TPM_MAX_ORDINAL)
360 duration_idx = tpm_ordinal_duration[ordinal];
361 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
362 TPM_MAX_PROTECTED_ORDINAL)
364 tpm_protected_ordinal_duration[ordinal &
365 TPM_PROTECTED_ORDINAL_MASK];
367 if (duration_idx != TPM_UNDEFINED)
368 duration = chip->vendor.duration[duration_idx];
374 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
377 * Internal kernel interface to transmit TPM commands
379 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
386 if (bufsiz > TPM_BUFSIZE)
387 bufsiz = TPM_BUFSIZE;
389 count = be32_to_cpu(*((__be32 *) (buf + 2)));
390 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
393 if (count > bufsiz) {
395 "invalid count value %x %zx \n", count, bufsiz);
399 mutex_lock(&chip->tpm_mutex);
401 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
403 "tpm_transmit: tpm_send: error %zd\n", rc);
407 if (chip->vendor.irq)
410 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
412 u8 status = chip->vendor.status(chip);
413 if ((status & chip->vendor.req_complete_mask) ==
414 chip->vendor.req_complete_val)
417 if ((status == chip->vendor.req_canceled)) {
418 dev_err(chip->dev, "Operation Canceled\n");
423 msleep(TPM_TIMEOUT); /* CHECK */
425 } while (time_before(jiffies, stop));
427 chip->vendor.cancel(chip);
428 dev_err(chip->dev, "Operation Timed out\n");
433 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
436 "tpm_transmit: tpm_recv: error %zd\n", rc);
438 mutex_unlock(&chip->tpm_mutex);
442 #define TPM_DIGEST_SIZE 20
443 #define TPM_ERROR_SIZE 10
444 #define TPM_RET_CODE_IDX 6
446 enum tpm_capabilities {
447 TPM_CAP_FLAG = cpu_to_be32(4),
448 TPM_CAP_PROP = cpu_to_be32(5),
449 CAP_VERSION_1_1 = cpu_to_be32(0x06),
450 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
453 enum tpm_sub_capabilities {
454 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
455 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
456 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
457 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
458 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
459 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
460 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
464 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
465 int len, const char *desc)
469 len = tpm_transmit(chip,(u8 *) cmd, len);
472 if (len == TPM_ERROR_SIZE) {
473 err = be32_to_cpu(cmd->header.out.return_code);
474 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
480 #define TPM_INTERNAL_RESULT_SIZE 200
481 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
482 #define TPM_ORD_GET_CAP cpu_to_be32(101)
484 static const struct tpm_input_header tpm_getcap_header = {
485 .tag = TPM_TAG_RQU_COMMAND,
486 .length = cpu_to_be32(22),
487 .ordinal = TPM_ORD_GET_CAP
490 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
493 struct tpm_cmd_t tpm_cmd;
495 struct tpm_chip *chip = dev_get_drvdata(dev);
497 tpm_cmd.header.in = tpm_getcap_header;
498 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
499 tpm_cmd.params.getcap_in.cap = subcap_id;
500 /*subcap field not necessary */
501 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
502 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
504 if (subcap_id == TPM_CAP_FLAG_PERM ||
505 subcap_id == TPM_CAP_FLAG_VOL)
506 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
508 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
509 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
510 tpm_cmd.params.getcap_in.subcap = subcap_id;
512 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
514 *cap = tpm_cmd.params.getcap_out.cap;
518 void tpm_gen_interrupt(struct tpm_chip *chip)
520 struct tpm_cmd_t tpm_cmd;
523 tpm_cmd.header.in = tpm_getcap_header;
524 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
525 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
526 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
528 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
529 "attempting to determine the timeouts");
531 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
533 void tpm_get_timeouts(struct tpm_chip *chip)
535 struct tpm_cmd_t tpm_cmd;
536 unsigned long new_timeout[4];
537 unsigned long old_timeout[4];
538 struct duration_t *duration_cap;
541 tpm_cmd.header.in = tpm_getcap_header;
542 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
543 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
544 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
546 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
547 "attempting to determine the timeouts");
551 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
552 be32_to_cpu(tpm_cmd.header.out.length)
553 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
556 old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a);
557 old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b);
558 old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c);
559 old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d);
560 memcpy(new_timeout, old_timeout, sizeof(new_timeout));
563 * Provide ability for vendor overrides of timeout values in case
566 if (chip->vendor.update_timeouts != NULL)
567 chip->vendor.timeout_adjusted =
568 chip->vendor.update_timeouts(chip, new_timeout);
570 if (!chip->vendor.timeout_adjusted) {
571 /* Don't overwrite default if value is 0 */
572 if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
575 /* timeouts in msec rather usec */
576 for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
577 new_timeout[i] *= 1000;
578 chip->vendor.timeout_adjusted = true;
582 /* Report adjusted timeouts */
583 if (chip->vendor.timeout_adjusted) {
585 HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
586 old_timeout[0], new_timeout[0],
587 old_timeout[1], new_timeout[1],
588 old_timeout[2], new_timeout[2],
589 old_timeout[3], new_timeout[3]);
592 chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]);
593 chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]);
594 chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]);
595 chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]);
598 tpm_cmd.header.in = tpm_getcap_header;
599 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
600 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
601 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
603 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
604 "attempting to determine the durations");
608 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
609 be32_to_cpu(tpm_cmd.header.out.length)
610 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
613 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
614 chip->vendor.duration[TPM_SHORT] =
615 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
616 chip->vendor.duration[TPM_MEDIUM] =
617 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
618 chip->vendor.duration[TPM_LONG] =
619 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
621 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
622 * value wrong and apparently reports msecs rather than usecs. So we
623 * fix up the resulting too-small TPM_SHORT value to make things work.
624 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
626 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
627 chip->vendor.duration[TPM_SHORT] = HZ;
628 chip->vendor.duration[TPM_MEDIUM] *= 1000;
629 chip->vendor.duration[TPM_LONG] *= 1000;
630 chip->vendor.duration_adjusted = true;
631 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
634 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
636 void tpm_continue_selftest(struct tpm_chip *chip)
639 0, 193, /* TPM_TAG_RQU_COMMAND */
640 0, 0, 0, 10, /* length */
641 0, 0, 0, 83, /* TPM_ORD_ContinueSelfTest */
644 tpm_transmit(chip, data, sizeof(data));
646 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
648 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
654 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
655 "attempting to determine the permanent enabled state");
659 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
662 EXPORT_SYMBOL_GPL(tpm_show_enabled);
664 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
670 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
671 "attempting to determine the permanent active state");
675 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
678 EXPORT_SYMBOL_GPL(tpm_show_active);
680 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
686 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
687 "attempting to determine the owner state");
691 rc = sprintf(buf, "%d\n", cap.owned);
694 EXPORT_SYMBOL_GPL(tpm_show_owned);
696 ssize_t tpm_show_temp_deactivated(struct device * dev,
697 struct device_attribute * attr, char *buf)
702 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
703 "attempting to determine the temporary state");
707 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
710 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
713 * tpm_chip_find_get - return tpm_chip for given chip number
715 static struct tpm_chip *tpm_chip_find_get(int chip_num)
717 struct tpm_chip *pos, *chip = NULL;
720 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
721 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
724 if (try_module_get(pos->dev->driver->owner)) {
733 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
734 #define READ_PCR_RESULT_SIZE 30
735 static struct tpm_input_header pcrread_header = {
736 .tag = TPM_TAG_RQU_COMMAND,
737 .length = cpu_to_be32(14),
738 .ordinal = TPM_ORDINAL_PCRREAD
741 int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
744 struct tpm_cmd_t cmd;
746 cmd.header.in = pcrread_header;
747 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
748 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
749 "attempting to read a pcr value");
752 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
758 * tpm_pcr_read - read a pcr value
759 * @chip_num: tpm idx # or ANY
760 * @pcr_idx: pcr idx to retrieve
761 * @res_buf: TPM_PCR value
762 * size of res_buf is 20 bytes (or NULL if you don't care)
764 * The TPM driver should be built-in, but for whatever reason it
765 * isn't, protect against the chip disappearing, by incrementing
766 * the module usage count.
768 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
770 struct tpm_chip *chip;
773 chip = tpm_chip_find_get(chip_num);
776 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
780 EXPORT_SYMBOL_GPL(tpm_pcr_read);
783 * tpm_pcr_extend - extend pcr value with hash
784 * @chip_num: tpm idx # or AN&
785 * @pcr_idx: pcr idx to extend
786 * @hash: hash value used to extend pcr value
788 * The TPM driver should be built-in, but for whatever reason it
789 * isn't, protect against the chip disappearing, by incrementing
790 * the module usage count.
792 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
793 #define EXTEND_PCR_RESULT_SIZE 34
794 static struct tpm_input_header pcrextend_header = {
795 .tag = TPM_TAG_RQU_COMMAND,
796 .length = cpu_to_be32(34),
797 .ordinal = TPM_ORD_PCR_EXTEND
800 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
802 struct tpm_cmd_t cmd;
804 struct tpm_chip *chip;
806 chip = tpm_chip_find_get(chip_num);
810 cmd.header.in = pcrextend_header;
811 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
812 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
813 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
814 "attempting extend a PCR value");
819 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
821 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
823 struct tpm_chip *chip;
826 chip = tpm_chip_find_get(chip_num);
830 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
835 EXPORT_SYMBOL_GPL(tpm_send);
837 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
841 u8 digest[TPM_DIGEST_SIZE];
845 struct tpm_chip *chip = dev_get_drvdata(dev);
847 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
848 "attempting to determine the number of PCRS");
852 num_pcrs = be32_to_cpu(cap.num_pcrs);
853 for (i = 0; i < num_pcrs; i++) {
854 rc = __tpm_pcr_read(chip, i, digest);
857 str += sprintf(str, "PCR-%02d: ", i);
858 for (j = 0; j < TPM_DIGEST_SIZE; j++)
859 str += sprintf(str, "%02X ", digest[j]);
860 str += sprintf(str, "\n");
864 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
866 #define READ_PUBEK_RESULT_SIZE 314
867 #define TPM_ORD_READPUBEK cpu_to_be32(124)
868 struct tpm_input_header tpm_readpubek_header = {
869 .tag = TPM_TAG_RQU_COMMAND,
870 .length = cpu_to_be32(30),
871 .ordinal = TPM_ORD_READPUBEK
874 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
878 struct tpm_cmd_t tpm_cmd;
882 struct tpm_chip *chip = dev_get_drvdata(dev);
884 memset(&tpm_cmd, 0, sizeof(tpm_cmd));
886 tpm_cmd.header.in = tpm_readpubek_header;
887 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
888 "attempting to read the PUBEK");
893 ignore header 10 bytes
894 algorithm 32 bits (1 == RSA )
897 parameters (RSA 12->bytes: keybit, #primes, expbit)
900 ignore checksum 20 bytes
902 data = tpm_cmd.params.readpubek_out_buffer;
905 "Algorithm: %02X %02X %02X %02X\n"
906 "Encscheme: %02X %02X\n"
907 "Sigscheme: %02X %02X\n"
908 "Parameters: %02X %02X %02X %02X "
909 "%02X %02X %02X %02X "
910 "%02X %02X %02X %02X\n"
911 "Modulus length: %d\n"
913 data[0], data[1], data[2], data[3],
916 data[12], data[13], data[14], data[15],
917 data[16], data[17], data[18], data[19],
918 data[20], data[21], data[22], data[23],
919 be32_to_cpu(*((__be32 *) (data + 24))));
921 for (i = 0; i < 256; i++) {
922 str += sprintf(str, "%02X ", data[i + 28]);
923 if ((i + 1) % 16 == 0)
924 str += sprintf(str, "\n");
930 EXPORT_SYMBOL_GPL(tpm_show_pubek);
933 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
940 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
941 "attempting to determine the manufacturer");
944 str += sprintf(str, "Manufacturer: 0x%x\n",
945 be32_to_cpu(cap.manufacturer_id));
947 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
948 "attempting to determine the 1.1 version");
952 "TCG version: %d.%d\nFirmware version: %d.%d\n",
953 cap.tpm_version.Major, cap.tpm_version.Minor,
954 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
957 EXPORT_SYMBOL_GPL(tpm_show_caps);
959 ssize_t tpm_show_caps_1_2(struct device * dev,
960 struct device_attribute * attr, char *buf)
966 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
967 "attempting to determine the manufacturer");
970 str += sprintf(str, "Manufacturer: 0x%x\n",
971 be32_to_cpu(cap.manufacturer_id));
972 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
973 "attempting to determine the 1.2 version");
977 "TCG version: %d.%d\nFirmware version: %d.%d\n",
978 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
979 cap.tpm_version_1_2.revMajor,
980 cap.tpm_version_1_2.revMinor);
983 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
985 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
988 struct tpm_chip *chip = dev_get_drvdata(dev);
990 if (chip->vendor.duration[TPM_LONG] == 0)
993 return sprintf(buf, "%d %d %d [%s]\n",
994 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
995 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
996 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
997 chip->vendor.duration_adjusted
998 ? "adjusted" : "original");
1000 EXPORT_SYMBOL_GPL(tpm_show_durations);
1002 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1005 struct tpm_chip *chip = dev_get_drvdata(dev);
1007 return sprintf(buf, "%d %d %d %d [%s]\n",
1008 jiffies_to_usecs(chip->vendor.timeout_a),
1009 jiffies_to_usecs(chip->vendor.timeout_b),
1010 jiffies_to_usecs(chip->vendor.timeout_c),
1011 jiffies_to_usecs(chip->vendor.timeout_d),
1012 chip->vendor.timeout_adjusted
1013 ? "adjusted" : "original");
1015 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1017 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1018 const char *buf, size_t count)
1020 struct tpm_chip *chip = dev_get_drvdata(dev);
1024 chip->vendor.cancel(chip);
1027 EXPORT_SYMBOL_GPL(tpm_store_cancel);
1030 * Device file system interface to the TPM
1032 * It's assured that the chip will be opened just once,
1033 * by the check of is_open variable, which is protected
1036 int tpm_open(struct inode *inode, struct file *file)
1038 int minor = iminor(inode);
1039 struct tpm_chip *chip = NULL, *pos;
1042 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1043 if (pos->vendor.miscdev.minor == minor) {
1045 get_device(chip->dev);
1054 if (test_and_set_bit(0, &chip->is_open)) {
1055 dev_dbg(chip->dev, "Another process owns this TPM\n");
1056 put_device(chip->dev);
1060 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1061 if (chip->data_buffer == NULL) {
1062 clear_bit(0, &chip->is_open);
1063 put_device(chip->dev);
1067 atomic_set(&chip->data_pending, 0);
1069 file->private_data = chip;
1072 EXPORT_SYMBOL_GPL(tpm_open);
1075 * Called on file close
1077 int tpm_release(struct inode *inode, struct file *file)
1079 struct tpm_chip *chip = file->private_data;
1081 del_singleshot_timer_sync(&chip->user_read_timer);
1082 flush_work_sync(&chip->work);
1083 file->private_data = NULL;
1084 atomic_set(&chip->data_pending, 0);
1085 kfree(chip->data_buffer);
1086 clear_bit(0, &chip->is_open);
1087 put_device(chip->dev);
1090 EXPORT_SYMBOL_GPL(tpm_release);
1092 ssize_t tpm_write(struct file *file, const char __user *buf,
1093 size_t size, loff_t *off)
1095 struct tpm_chip *chip = file->private_data;
1096 size_t in_size = size;
1099 /* cannot perform a write until the read has cleared
1100 either via tpm_read or a user_read_timer timeout.
1101 This also prevents splitted buffered writes from blocking here.
1103 if (atomic_read(&chip->data_pending) != 0)
1106 if (in_size > TPM_BUFSIZE)
1109 mutex_lock(&chip->buffer_mutex);
1112 (chip->data_buffer, (void __user *) buf, in_size)) {
1113 mutex_unlock(&chip->buffer_mutex);
1118 in_size < be32_to_cpu(*((__be32 *) (chip->data_buffer + 2)))) {
1119 mutex_unlock(&chip->buffer_mutex);
1123 /* atomic tpm command send and result receive */
1124 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1126 mutex_unlock(&chip->buffer_mutex);
1130 atomic_set(&chip->data_pending, out_size);
1131 mutex_unlock(&chip->buffer_mutex);
1133 /* Set a timeout by which the reader must come claim the result */
1134 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1138 EXPORT_SYMBOL_GPL(tpm_write);
1140 ssize_t tpm_read(struct file *file, char __user *buf,
1141 size_t size, loff_t *off)
1143 struct tpm_chip *chip = file->private_data;
1147 del_singleshot_timer_sync(&chip->user_read_timer);
1148 flush_work_sync(&chip->work);
1149 ret_size = atomic_read(&chip->data_pending);
1150 atomic_set(&chip->data_pending, 0);
1151 if (ret_size > 0) { /* relay data */
1152 ssize_t orig_ret_size = ret_size;
1153 if (size < ret_size)
1156 mutex_lock(&chip->buffer_mutex);
1157 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1158 memset(chip->data_buffer, 0, orig_ret_size);
1162 mutex_unlock(&chip->buffer_mutex);
1167 EXPORT_SYMBOL_GPL(tpm_read);
1169 void tpm_remove_hardware(struct device *dev)
1171 struct tpm_chip *chip = dev_get_drvdata(dev);
1174 dev_err(dev, "No device data found\n");
1178 spin_lock(&driver_lock);
1179 list_del_rcu(&chip->list);
1180 spin_unlock(&driver_lock);
1183 misc_deregister(&chip->vendor.miscdev);
1184 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1185 tpm_bios_log_teardown(chip->bios_dir);
1187 /* write it this way to be explicit (chip->dev == dev) */
1188 put_device(chip->dev);
1190 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1192 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1193 #define SAVESTATE_RESULT_SIZE 10
1195 static struct tpm_input_header savestate_header = {
1196 .tag = TPM_TAG_RQU_COMMAND,
1197 .length = cpu_to_be32(10),
1198 .ordinal = TPM_ORD_SAVESTATE
1202 * We are about to suspend. Save the TPM state
1203 * so that it can be restored.
1205 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1207 struct tpm_chip *chip = dev_get_drvdata(dev);
1208 struct tpm_cmd_t cmd;
1211 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1216 /* for buggy tpm, flush pcrs with extend to selected dummy */
1217 if (tpm_suspend_pcr) {
1218 cmd.header.in = pcrextend_header;
1219 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1220 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1222 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1223 "extending dummy pcr before suspend");
1226 /* now do the actual savestate */
1227 cmd.header.in = savestate_header;
1228 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1229 "sending savestate before suspend");
1232 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1235 * Resume from a power safe. The BIOS already restored
1238 int tpm_pm_resume(struct device *dev)
1240 struct tpm_chip *chip = dev_get_drvdata(dev);
1247 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1249 /* In case vendor provided release function, call it too.*/
1251 void tpm_dev_vendor_release(struct tpm_chip *chip)
1253 if (chip->vendor.release)
1254 chip->vendor.release(chip->dev);
1256 clear_bit(chip->dev_num, dev_mask);
1257 kfree(chip->vendor.miscdev.name);
1259 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1263 * Once all references to platform device are down to 0,
1264 * release all allocated structures.
1266 void tpm_dev_release(struct device *dev)
1268 struct tpm_chip *chip = dev_get_drvdata(dev);
1270 tpm_dev_vendor_release(chip);
1275 EXPORT_SYMBOL_GPL(tpm_dev_release);
1278 * Called from tpm_<specific>.c probe function only for devices
1279 * the driver has determined it should claim. Prior to calling
1280 * this function the specific probe function has called pci_enable_device
1281 * upon errant exit from this function specific probe function should call
1282 * pci_disable_device
1284 struct tpm_chip *tpm_register_hardware(struct device *dev,
1285 const struct tpm_vendor_specific *entry)
1287 #define DEVNAME_SIZE 7
1290 struct tpm_chip *chip;
1292 /* Driver specific per-device data */
1293 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1294 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1296 if (chip == NULL || devname == NULL)
1299 mutex_init(&chip->buffer_mutex);
1300 mutex_init(&chip->tpm_mutex);
1301 INIT_LIST_HEAD(&chip->list);
1303 INIT_WORK(&chip->work, timeout_work);
1305 setup_timer(&chip->user_read_timer, user_reader_timeout,
1306 (unsigned long)chip);
1308 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1310 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1312 if (chip->dev_num >= TPM_NUM_DEVICES) {
1313 dev_err(dev, "No available tpm device numbers\n");
1315 } else if (chip->dev_num == 0)
1316 chip->vendor.miscdev.minor = TPM_MINOR;
1318 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1320 set_bit(chip->dev_num, dev_mask);
1322 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1323 chip->vendor.miscdev.name = devname;
1325 chip->vendor.miscdev.parent = dev;
1326 chip->dev = get_device(dev);
1327 chip->release = dev->release;
1328 dev->release = tpm_dev_release;
1329 dev_set_drvdata(dev, chip);
1331 if (misc_register(&chip->vendor.miscdev)) {
1333 "unable to misc_register %s, minor %d\n",
1334 chip->vendor.miscdev.name,
1335 chip->vendor.miscdev.minor);
1336 put_device(chip->dev);
1340 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1341 misc_deregister(&chip->vendor.miscdev);
1342 put_device(chip->dev);
1347 chip->bios_dir = tpm_bios_log_setup(devname);
1349 /* Make chip available */
1350 spin_lock(&driver_lock);
1351 list_add_tail_rcu(&chip->list, &tpm_chip_list);
1352 spin_unlock(&driver_lock);
1361 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1363 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1364 MODULE_DESCRIPTION("TPM Driver");
1365 MODULE_VERSION("2.0");
1366 MODULE_LICENSE("GPL");