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/jiffies.h>
29 #include <linux/mutex.h>
30 #include <linux/spinlock.h>
31 #include <linux/freezer.h>
36 TPM_MINOR = 224, /* officially assigned */
38 TPM_NUM_DEVICES = 256,
48 #define TPM_MAX_ORDINAL 243
49 #define TPM_MAX_PROTECTED_ORDINAL 12
50 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
53 * Bug workaround - some TPM's don't flush the most
54 * recently changed pcr on suspend, so force the flush
55 * with an extend to the selected _unused_ non-volatile pcr.
57 static int tpm_suspend_pcr;
58 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
59 MODULE_PARM_DESC(suspend_pcr,
60 "PCR to use for dummy writes to faciltate flush on suspend.");
62 static LIST_HEAD(tpm_chip_list);
63 static DEFINE_SPINLOCK(driver_lock);
64 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
67 * Array with one entry per ordinal defining the maximum amount
68 * of time the chip could take to return the result. The ordinal
69 * designation of short, medium or long is defined in a table in
70 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
71 * values of the SHORT, MEDIUM, and LONG durations are retrieved
72 * from the chip during initialization with a call to tpm_get_timeouts.
74 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
75 TPM_UNDEFINED, /* 0 */
80 TPM_UNDEFINED, /* 5 */
89 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
90 TPM_UNDEFINED, /* 0 */
95 TPM_UNDEFINED, /* 5 */
145 TPM_UNDEFINED, /* 55 */
165 TPM_UNDEFINED, /* 75 */
175 TPM_UNDEFINED, /* 85 */
185 TPM_UNDEFINED, /* 95 */
190 TPM_MEDIUM, /* 100 */
195 TPM_UNDEFINED, /* 105 */
225 TPM_UNDEFINED, /* 135 */
235 TPM_UNDEFINED, /* 145 */
245 TPM_UNDEFINED, /* 155 */
255 TPM_UNDEFINED, /* 165 */
265 TPM_UNDEFINED, /* 175 */
270 TPM_MEDIUM, /* 180 */
275 TPM_MEDIUM, /* 185 */
280 TPM_UNDEFINED, /* 190 */
285 TPM_UNDEFINED, /* 195 */
300 TPM_MEDIUM, /* 210 */
305 TPM_UNDEFINED, /* 215 */
315 TPM_UNDEFINED, /* 225 */
325 TPM_UNDEFINED, /* 235 */
335 static void user_reader_timeout(unsigned long ptr)
337 struct tpm_chip *chip = (struct tpm_chip *) ptr;
339 schedule_work(&chip->work);
342 static void timeout_work(struct work_struct *work)
344 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
346 mutex_lock(&chip->buffer_mutex);
347 atomic_set(&chip->data_pending, 0);
348 memset(chip->data_buffer, 0, TPM_BUFSIZE);
349 mutex_unlock(&chip->buffer_mutex);
352 static void set_needs_resume(struct tpm_chip *chip)
354 mutex_lock(&chip->resume_mutex);
355 chip->resume_time = jiffies;
356 chip->needs_resume = 1;
357 mutex_unlock(&chip->resume_mutex);
360 #define TPM_ORD_CONTINUE_SELFTEST 83
361 #define CONTINUE_SELFTEST_RESULT_SIZE 10
363 #define TPM_INTERNAL_RESULT_SIZE 200
364 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
365 #define TPM_ORD_GET_CAP cpu_to_be32(101)
367 static const struct tpm_input_header tpm_getcap_header = {
368 .tag = TPM_TAG_RQU_COMMAND,
369 .length = cpu_to_be32(22),
370 .ordinal = TPM_ORD_GET_CAP
373 static struct tpm_input_header continue_selftest_header = {
374 .tag = TPM_TAG_RQU_COMMAND,
375 .length = cpu_to_be32(10),
376 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
380 * Internal kernel interface to transmit TPM commands
382 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
389 if (bufsiz > TPM_BUFSIZE)
390 bufsiz = TPM_BUFSIZE;
392 count = be32_to_cpu(*((__be32 *) (buf + 2)));
393 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
396 if (count > bufsiz) {
398 "invalid count value %x %zx \n", count, bufsiz);
402 mutex_lock(&chip->tpm_mutex);
404 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
406 "tpm_transmit: tpm_send: error %zd\n", rc);
410 if (chip->vendor.irq)
413 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
415 u8 status = chip->vendor.status(chip);
416 if ((status & chip->vendor.req_complete_mask) ==
417 chip->vendor.req_complete_val)
420 if ((status == chip->vendor.req_canceled)) {
421 dev_err(chip->dev, "Operation Canceled\n");
426 msleep(TPM_TIMEOUT); /* CHECK */
428 } while (time_before(jiffies, stop));
430 chip->vendor.cancel(chip);
431 dev_err(chip->dev, "Operation Timed out\n");
436 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
439 "tpm_transmit: tpm_recv: error %zd\n", rc);
441 mutex_unlock(&chip->tpm_mutex);
445 void tpm_continue_selftest_nocheck(struct tpm_chip *chip)
447 struct tpm_cmd_t cmd;
448 cmd.header.in = continue_selftest_header;
449 tpm_transmit(chip, (u8 *) &cmd, CONTINUE_SELFTEST_RESULT_SIZE);
452 /* The maximum time in milliseconds that the TPM self test will take to
453 * complete. TODO(semenzato): 1s should be plenty for all TPMs, but how can we
456 #define TPM_SELF_TEST_DURATION_MSEC 1000
458 /* We don't want to wait for the self test to complete at resume, because it
459 * impacts the resume speed. TPM commands are infrequent so the wait is
460 * usually not needed and is wasteful. Instead, before we send any command, we
461 * check that enough time has elapsed from the resume so that we are
462 * comfortable that the self test has completed. If not, we wait. Unlike at
463 * boot, here we don't check the return code of continue_self_test, so we can
464 * use a code path which avoids recursion. Furthermore, this only works when
465 * ContinueSelfTest is blocking, that is it returns only after the self test
466 * has completed, which is the case for the Infineon TPM.
468 static void resume_if_needed(struct tpm_chip *chip)
470 mutex_lock(&chip->resume_mutex);
471 if (chip->needs_resume) {
472 /* If it's been TPM_SELF_TEST_DURATION_MSEC msec since resume,
473 * then selftest has completed and we don't need to wait.
475 if (jiffies - chip->resume_time <
476 msecs_to_jiffies(TPM_SELF_TEST_DURATION_MSEC)) {
477 dev_info(chip->dev, "waiting for TPM self test");
478 tpm_continue_selftest_nocheck(chip);
480 chip->needs_resume = 0;
481 dev_info(chip->dev, "TPM delayed resume completed");
483 mutex_unlock(&chip->resume_mutex);
486 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
487 int len, const char *desc)
491 resume_if_needed(chip);
493 len = tpm_transmit(chip,(u8 *) cmd, len);
496 else if (len < TPM_HEADER_SIZE)
499 err = be32_to_cpu(cmd->header.out.return_code);
501 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
507 * tpm_continue_selftest -- run TPM's selftest
508 * @chip: TPM chip to use
510 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
513 int tpm_continue_selftest(struct tpm_chip *chip)
516 struct tpm_cmd_t cmd;
518 cmd.header.in = continue_selftest_header;
519 rc = transmit_cmd(chip, (const u8 *) &cmd,
520 CONTINUE_SELFTEST_RESULT_SIZE, "continue selftest");
523 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
526 * Returns max number of jiffies to wait
528 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
531 int duration_idx = TPM_UNDEFINED;
534 if (ordinal < TPM_MAX_ORDINAL)
535 duration_idx = tpm_ordinal_duration[ordinal];
536 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
537 TPM_MAX_PROTECTED_ORDINAL)
539 tpm_protected_ordinal_duration[ordinal &
540 TPM_PROTECTED_ORDINAL_MASK];
542 if (duration_idx != TPM_UNDEFINED)
543 duration = chip->vendor.duration[duration_idx];
549 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
551 #define TPM_DIGEST_SIZE 20
552 #define TPM_RET_CODE_IDX 6
554 enum tpm_capabilities {
555 TPM_CAP_FLAG = cpu_to_be32(4),
556 TPM_CAP_PROP = cpu_to_be32(5),
557 CAP_VERSION_1_1 = cpu_to_be32(0x06),
558 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
561 enum tpm_sub_capabilities {
562 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
563 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
564 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
565 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
566 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
567 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
568 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
572 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
575 struct tpm_cmd_t tpm_cmd;
577 struct tpm_chip *chip = dev_get_drvdata(dev);
579 tpm_cmd.header.in = tpm_getcap_header;
580 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
581 tpm_cmd.params.getcap_in.cap = subcap_id;
582 /*subcap field not necessary */
583 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
584 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
586 if (subcap_id == TPM_CAP_FLAG_PERM ||
587 subcap_id == TPM_CAP_FLAG_VOL)
588 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
590 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
591 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
592 tpm_cmd.params.getcap_in.subcap = subcap_id;
594 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
596 *cap = tpm_cmd.params.getcap_out.cap;
600 void tpm_gen_interrupt(struct tpm_chip *chip)
602 struct tpm_cmd_t tpm_cmd;
605 tpm_cmd.header.in = tpm_getcap_header;
606 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
607 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
608 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
610 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
611 "attempting to determine the timeouts");
613 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
615 int tpm_get_timeouts(struct tpm_chip *chip)
617 struct tpm_cmd_t tpm_cmd;
618 struct timeout_t *timeout_cap;
619 struct duration_t *duration_cap;
622 unsigned int scale = 1;
624 tpm_cmd.header.in = tpm_getcap_header;
625 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
626 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
627 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
629 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
630 "attempting to determine the timeouts");
634 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
635 be32_to_cpu(tpm_cmd.header.out.length)
636 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
639 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
640 /* Don't overwrite default if value is 0 */
641 timeout = be32_to_cpu(timeout_cap->a);
642 if (timeout && timeout < 1000) {
643 /* timeouts in msec rather usec */
645 chip->vendor.timeout_adjusted = true;
648 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
649 timeout = be32_to_cpu(timeout_cap->b);
651 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
652 timeout = be32_to_cpu(timeout_cap->c);
654 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
655 timeout = be32_to_cpu(timeout_cap->d);
657 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
660 tpm_cmd.header.in = tpm_getcap_header;
661 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
662 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
663 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
665 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
666 "attempting to determine the durations");
670 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
671 be32_to_cpu(tpm_cmd.header.out.length)
672 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
675 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
676 chip->vendor.duration[TPM_SHORT] =
677 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
678 chip->vendor.duration[TPM_MEDIUM] =
679 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
680 chip->vendor.duration[TPM_LONG] =
681 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
683 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
684 * value wrong and apparently reports msecs rather than usecs. So we
685 * fix up the resulting too-small TPM_SHORT value to make things work.
686 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
688 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
689 chip->vendor.duration[TPM_SHORT] = HZ;
690 chip->vendor.duration[TPM_MEDIUM] *= 1000;
691 chip->vendor.duration[TPM_LONG] *= 1000;
692 chip->vendor.duration_adjusted = true;
693 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
697 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
699 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
705 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
706 "attempting to determine the permanent enabled state");
710 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
713 EXPORT_SYMBOL_GPL(tpm_show_enabled);
715 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
721 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
722 "attempting to determine the permanent active state");
726 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
729 EXPORT_SYMBOL_GPL(tpm_show_active);
731 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
737 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
738 "attempting to determine the owner state");
742 rc = sprintf(buf, "%d\n", cap.owned);
745 EXPORT_SYMBOL_GPL(tpm_show_owned);
747 ssize_t tpm_show_temp_deactivated(struct device * dev,
748 struct device_attribute * attr, char *buf)
753 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
754 "attempting to determine the temporary state");
758 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
761 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
764 * tpm_chip_find_get - return tpm_chip for given chip number
766 static struct tpm_chip *tpm_chip_find_get(int chip_num)
768 struct tpm_chip *pos, *chip = NULL;
771 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
772 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
775 if (try_module_get(pos->dev->driver->owner)) {
784 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
785 #define READ_PCR_RESULT_SIZE 30
786 static struct tpm_input_header pcrread_header = {
787 .tag = TPM_TAG_RQU_COMMAND,
788 .length = cpu_to_be32(14),
789 .ordinal = TPM_ORDINAL_PCRREAD
792 static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
795 struct tpm_cmd_t cmd;
797 cmd.header.in = pcrread_header;
798 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
799 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
800 "attempting to read a pcr value");
803 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
809 * tpm_pcr_read - read a pcr value
810 * @chip_num: tpm idx # or ANY
811 * @pcr_idx: pcr idx to retrieve
812 * @res_buf: TPM_PCR value
813 * size of res_buf is 20 bytes (or NULL if you don't care)
815 * The TPM driver should be built-in, but for whatever reason it
816 * isn't, protect against the chip disappearing, by incrementing
817 * the module usage count.
819 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
821 struct tpm_chip *chip;
824 chip = tpm_chip_find_get(chip_num);
827 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
831 EXPORT_SYMBOL_GPL(tpm_pcr_read);
834 * tpm_pcr_extend - extend pcr value with hash
835 * @chip_num: tpm idx # or AN&
836 * @pcr_idx: pcr idx to extend
837 * @hash: hash value used to extend pcr value
839 * The TPM driver should be built-in, but for whatever reason it
840 * isn't, protect against the chip disappearing, by incrementing
841 * the module usage count.
843 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
844 #define EXTEND_PCR_RESULT_SIZE 34
845 static struct tpm_input_header pcrextend_header = {
846 .tag = TPM_TAG_RQU_COMMAND,
847 .length = cpu_to_be32(34),
848 .ordinal = TPM_ORD_PCR_EXTEND
851 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
853 struct tpm_cmd_t cmd;
855 struct tpm_chip *chip;
857 chip = tpm_chip_find_get(chip_num);
861 cmd.header.in = pcrextend_header;
862 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
863 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
864 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
865 "attempting extend a PCR value");
870 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
873 * tpm_do_selftest - have the TPM continue its selftest and wait until it
874 * can receive further commands
875 * @chip: TPM chip to use
877 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
880 int tpm_do_selftest(struct tpm_chip *chip)
883 u8 digest[TPM_DIGEST_SIZE];
885 unsigned int delay_msec = 1000;
886 unsigned long duration;
888 duration = tpm_calc_ordinal_duration(chip,
889 TPM_ORD_CONTINUE_SELFTEST);
891 loops = jiffies_to_msecs(duration) / delay_msec;
893 rc = tpm_continue_selftest(chip);
894 /* This may fail if there was no TPM driver during a suspend/resume
895 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
901 rc = __tpm_pcr_read(chip, 0, digest);
902 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
904 "TPM is disabled/deactivated (0x%X)\n", rc);
905 /* TPM is disabled and/or deactivated; driver can
906 * proceed and TPM does handle commands for
907 * suspend/resume correctly
911 if (rc != TPM_WARN_DOING_SELFTEST)
914 } while (--loops > 0);
918 EXPORT_SYMBOL_GPL(tpm_do_selftest);
920 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
922 struct tpm_chip *chip;
925 chip = tpm_chip_find_get(chip_num);
929 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
934 EXPORT_SYMBOL_GPL(tpm_send);
936 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
940 u8 digest[TPM_DIGEST_SIZE];
944 struct tpm_chip *chip = dev_get_drvdata(dev);
946 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
947 "attempting to determine the number of PCRS");
951 num_pcrs = be32_to_cpu(cap.num_pcrs);
952 for (i = 0; i < num_pcrs; i++) {
953 rc = __tpm_pcr_read(chip, i, digest);
956 str += sprintf(str, "PCR-%02d: ", i);
957 for (j = 0; j < TPM_DIGEST_SIZE; j++)
958 str += sprintf(str, "%02X ", digest[j]);
959 str += sprintf(str, "\n");
963 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
965 #define READ_PUBEK_RESULT_SIZE 314
966 #define TPM_ORD_READPUBEK cpu_to_be32(124)
967 struct tpm_input_header tpm_readpubek_header = {
968 .tag = TPM_TAG_RQU_COMMAND,
969 .length = cpu_to_be32(30),
970 .ordinal = TPM_ORD_READPUBEK
973 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
977 struct tpm_cmd_t tpm_cmd;
982 struct tpm_chip *chip = dev_get_drvdata(dev);
984 tpm_cmd.header.in = tpm_readpubek_header;
985 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
986 "attempting to read the PUBEK");
991 ignore header 10 bytes
992 algorithm 32 bits (1 == RSA )
995 parameters (RSA 12->bytes: keybit, #primes, expbit)
998 ignore checksum 20 bytes
1000 data = tpm_cmd.params.readpubek_out_buffer;
1003 "Algorithm: %02X %02X %02X %02X\n"
1004 "Encscheme: %02X %02X\n"
1005 "Sigscheme: %02X %02X\n"
1006 "Parameters: %02X %02X %02X %02X "
1007 "%02X %02X %02X %02X "
1008 "%02X %02X %02X %02X\n"
1009 "Modulus length: %d\n"
1011 data[0], data[1], data[2], data[3],
1014 data[12], data[13], data[14], data[15],
1015 data[16], data[17], data[18], data[19],
1016 data[20], data[21], data[22], data[23],
1017 be32_to_cpu(*((__be32 *) (data + 24))));
1019 for (i = 0; i < 256; i++) {
1020 str += sprintf(str, "%02X ", data[i + 28]);
1021 if ((i + 1) % 16 == 0)
1022 str += sprintf(str, "\n");
1028 EXPORT_SYMBOL_GPL(tpm_show_pubek);
1031 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
1038 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1039 "attempting to determine the manufacturer");
1042 str += sprintf(str, "Manufacturer: 0x%x\n",
1043 be32_to_cpu(cap.manufacturer_id));
1045 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
1046 "attempting to determine the 1.1 version");
1050 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1051 cap.tpm_version.Major, cap.tpm_version.Minor,
1052 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1055 EXPORT_SYMBOL_GPL(tpm_show_caps);
1057 ssize_t tpm_show_caps_1_2(struct device * dev,
1058 struct device_attribute * attr, char *buf)
1064 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1065 "attempting to determine the manufacturer");
1068 str += sprintf(str, "Manufacturer: 0x%x\n",
1069 be32_to_cpu(cap.manufacturer_id));
1070 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1071 "attempting to determine the 1.2 version");
1075 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1076 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1077 cap.tpm_version_1_2.revMajor,
1078 cap.tpm_version_1_2.revMinor);
1081 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1083 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1086 struct tpm_chip *chip = dev_get_drvdata(dev);
1088 if (chip->vendor.duration[TPM_LONG] == 0)
1091 return sprintf(buf, "%d %d %d [%s]\n",
1092 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1093 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1094 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1095 chip->vendor.duration_adjusted
1096 ? "adjusted" : "original");
1098 EXPORT_SYMBOL_GPL(tpm_show_durations);
1100 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1103 struct tpm_chip *chip = dev_get_drvdata(dev);
1105 return sprintf(buf, "%d %d %d %d [%s]\n",
1106 jiffies_to_usecs(chip->vendor.timeout_a),
1107 jiffies_to_usecs(chip->vendor.timeout_b),
1108 jiffies_to_usecs(chip->vendor.timeout_c),
1109 jiffies_to_usecs(chip->vendor.timeout_d),
1110 chip->vendor.timeout_adjusted
1111 ? "adjusted" : "original");
1113 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1115 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1116 const char *buf, size_t count)
1118 struct tpm_chip *chip = dev_get_drvdata(dev);
1122 chip->vendor.cancel(chip);
1125 EXPORT_SYMBOL_GPL(tpm_store_cancel);
1127 int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1128 wait_queue_head_t *queue)
1134 /* check current status */
1135 status = chip->vendor.status(chip);
1136 if ((status & mask) == mask)
1139 stop = jiffies + timeout;
1141 if (chip->vendor.irq) {
1143 timeout = stop - jiffies;
1144 if ((long)timeout <= 0)
1146 rc = wait_event_interruptible_timeout(*queue,
1147 ((chip->vendor.status(chip)
1152 if (rc == -ERESTARTSYS && freezing(current)) {
1153 clear_thread_flag(TIF_SIGPENDING);
1158 msleep(TPM_TIMEOUT);
1159 status = chip->vendor.status(chip);
1160 if ((status & mask) == mask)
1162 } while (time_before(jiffies, stop));
1166 EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1168 * Device file system interface to the TPM
1170 * It's assured that the chip will be opened just once,
1171 * by the check of is_open variable, which is protected
1174 int tpm_open(struct inode *inode, struct file *file)
1176 int minor = iminor(inode);
1177 struct tpm_chip *chip = NULL, *pos;
1180 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1181 if (pos->vendor.miscdev.minor == minor) {
1183 get_device(chip->dev);
1192 if (test_and_set_bit(0, &chip->is_open)) {
1193 dev_dbg(chip->dev, "Another process owns this TPM\n");
1194 put_device(chip->dev);
1198 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1199 if (chip->data_buffer == NULL) {
1200 clear_bit(0, &chip->is_open);
1201 put_device(chip->dev);
1205 atomic_set(&chip->data_pending, 0);
1207 file->private_data = chip;
1210 EXPORT_SYMBOL_GPL(tpm_open);
1213 * Called on file close
1215 int tpm_release(struct inode *inode, struct file *file)
1217 struct tpm_chip *chip = file->private_data;
1219 del_singleshot_timer_sync(&chip->user_read_timer);
1220 flush_work_sync(&chip->work);
1221 file->private_data = NULL;
1222 atomic_set(&chip->data_pending, 0);
1223 kfree(chip->data_buffer);
1224 clear_bit(0, &chip->is_open);
1225 put_device(chip->dev);
1228 EXPORT_SYMBOL_GPL(tpm_release);
1230 ssize_t tpm_write(struct file *file, const char __user *buf,
1231 size_t size, loff_t *off)
1233 struct tpm_chip *chip = file->private_data;
1234 size_t in_size = size, out_size;
1236 /* cannot perform a write until the read has cleared
1237 either via tpm_read or a user_read_timer timeout */
1238 while (atomic_read(&chip->data_pending) != 0)
1239 msleep(TPM_TIMEOUT);
1241 resume_if_needed(chip);
1243 mutex_lock(&chip->buffer_mutex);
1245 if (in_size > TPM_BUFSIZE)
1246 in_size = TPM_BUFSIZE;
1249 (chip->data_buffer, (void __user *) buf, in_size)) {
1250 mutex_unlock(&chip->buffer_mutex);
1254 /* atomic tpm command send and result receive */
1255 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1257 atomic_set(&chip->data_pending, out_size);
1258 mutex_unlock(&chip->buffer_mutex);
1260 /* Set a timeout by which the reader must come claim the result */
1261 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1265 EXPORT_SYMBOL_GPL(tpm_write);
1267 ssize_t tpm_read(struct file *file, char __user *buf,
1268 size_t size, loff_t *off)
1270 struct tpm_chip *chip = file->private_data;
1274 del_singleshot_timer_sync(&chip->user_read_timer);
1275 flush_work_sync(&chip->work);
1276 ret_size = atomic_read(&chip->data_pending);
1277 /* TODO(wad): atomic_set should come AFTER the buffer is copied. */
1278 atomic_set(&chip->data_pending, 0);
1279 if (ret_size > 0) { /* relay data */
1280 ssize_t orig_ret_size = ret_size;
1281 if (size < ret_size)
1284 mutex_lock(&chip->buffer_mutex);
1285 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1286 memset(chip->data_buffer, 0, orig_ret_size);
1290 mutex_unlock(&chip->buffer_mutex);
1295 EXPORT_SYMBOL_GPL(tpm_read);
1297 void tpm_remove_hardware(struct device *dev)
1299 struct tpm_chip *chip = dev_get_drvdata(dev);
1302 dev_err(dev, "No device data found\n");
1306 spin_lock(&driver_lock);
1307 list_del_rcu(&chip->list);
1308 spin_unlock(&driver_lock);
1311 misc_deregister(&chip->vendor.miscdev);
1312 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1313 tpm_bios_log_teardown(chip->bios_dir);
1315 /* write it this way to be explicit (chip->dev == dev) */
1316 put_device(chip->dev);
1318 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1320 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1321 #define SAVESTATE_RESULT_SIZE 10
1323 static struct tpm_input_header savestate_header = {
1324 .tag = TPM_TAG_RQU_COMMAND,
1325 .length = cpu_to_be32(10),
1326 .ordinal = TPM_ORD_SAVESTATE
1330 * We are about to suspend. Save the TPM state
1331 * so that it can be restored.
1333 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1335 struct tpm_chip *chip = dev_get_drvdata(dev);
1336 struct tpm_cmd_t cmd;
1339 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1344 /* for buggy tpm, flush pcrs with extend to selected dummy */
1345 if (tpm_suspend_pcr) {
1346 cmd.header.in = pcrextend_header;
1347 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1348 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1350 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1351 "extending dummy pcr before suspend");
1354 /* now do the actual savestate */
1355 cmd.header.in = savestate_header;
1356 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1357 "sending savestate before suspend");
1360 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1363 * Resume from a power safe. The BIOS already restored
1366 int tpm_pm_resume(struct device *dev)
1368 struct tpm_chip *chip = dev_get_drvdata(dev);
1373 set_needs_resume(chip);
1376 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1378 /* In case vendor provided release function, call it too.*/
1380 void tpm_dev_vendor_release(struct tpm_chip *chip)
1382 if (chip->vendor.release)
1383 chip->vendor.release(chip->dev);
1385 clear_bit(chip->dev_num, dev_mask);
1386 kfree(chip->vendor.miscdev.name);
1388 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1392 * Once all references to platform device are down to 0,
1393 * release all allocated structures.
1395 void tpm_dev_release(struct device *dev)
1397 struct tpm_chip *chip = dev_get_drvdata(dev);
1399 tpm_dev_vendor_release(chip);
1404 EXPORT_SYMBOL_GPL(tpm_dev_release);
1407 * Called from tpm_<specific>.c probe function only for devices
1408 * the driver has determined it should claim. Prior to calling
1409 * this function the specific probe function has called pci_enable_device
1410 * upon errant exit from this function specific probe function should call
1411 * pci_disable_device
1413 struct tpm_chip *tpm_register_hardware(struct device *dev,
1414 const struct tpm_vendor_specific *entry)
1416 #define DEVNAME_SIZE 7
1419 struct tpm_chip *chip;
1421 /* Driver specific per-device data */
1422 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1423 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1425 if (chip == NULL || devname == NULL)
1428 mutex_init(&chip->buffer_mutex);
1429 mutex_init(&chip->tpm_mutex);
1430 mutex_init(&chip->resume_mutex);
1431 INIT_LIST_HEAD(&chip->list);
1433 INIT_WORK(&chip->work, timeout_work);
1435 setup_timer(&chip->user_read_timer, user_reader_timeout,
1436 (unsigned long)chip);
1438 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1440 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1442 if (chip->dev_num >= TPM_NUM_DEVICES) {
1443 dev_err(dev, "No available tpm device numbers\n");
1445 } else if (chip->dev_num == 0)
1446 chip->vendor.miscdev.minor = TPM_MINOR;
1448 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1450 set_bit(chip->dev_num, dev_mask);
1452 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1453 chip->vendor.miscdev.name = devname;
1455 chip->vendor.miscdev.parent = dev;
1456 chip->dev = get_device(dev);
1457 chip->release = dev->release;
1458 dev->release = tpm_dev_release;
1459 dev_set_drvdata(dev, chip);
1461 if (misc_register(&chip->vendor.miscdev)) {
1463 "unable to misc_register %s, minor %d\n",
1464 chip->vendor.miscdev.name,
1465 chip->vendor.miscdev.minor);
1466 put_device(chip->dev);
1470 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1471 misc_deregister(&chip->vendor.miscdev);
1472 put_device(chip->dev);
1477 chip->bios_dir = tpm_bios_log_setup(devname);
1479 /* Make chip available */
1480 spin_lock(&driver_lock);
1481 list_add_rcu(&chip->list, &tpm_chip_list);
1482 spin_unlock(&driver_lock);
1491 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1493 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1494 MODULE_DESCRIPTION("TPM Driver");
1495 MODULE_VERSION("2.0");
1496 MODULE_LICENSE("GPL");