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>
30 #include <linux/freezer.h>
35 TPM_MINOR = 224, /* officially assigned */
37 TPM_NUM_DEVICES = 256,
47 #define TPM_MAX_ORDINAL 243
48 #define TPM_MAX_PROTECTED_ORDINAL 12
49 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
52 * Bug workaround - some TPM's don't flush the most
53 * recently changed pcr on suspend, so force the flush
54 * with an extend to the selected _unused_ non-volatile pcr.
56 static int tpm_suspend_pcr;
57 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
58 MODULE_PARM_DESC(suspend_pcr,
59 "PCR to use for dummy writes to faciltate flush on suspend.");
61 static LIST_HEAD(tpm_chip_list);
62 static DEFINE_SPINLOCK(driver_lock);
63 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
66 * Array with one entry per ordinal defining the maximum amount
67 * of time the chip could take to return the result. The ordinal
68 * designation of short, medium or long is defined in a table in
69 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
70 * values of the SHORT, MEDIUM, and LONG durations are retrieved
71 * from the chip during initialization with a call to tpm_get_timeouts.
73 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
74 TPM_UNDEFINED, /* 0 */
79 TPM_UNDEFINED, /* 5 */
88 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
89 TPM_UNDEFINED, /* 0 */
94 TPM_UNDEFINED, /* 5 */
144 TPM_UNDEFINED, /* 55 */
164 TPM_UNDEFINED, /* 75 */
174 TPM_UNDEFINED, /* 85 */
184 TPM_UNDEFINED, /* 95 */
189 TPM_MEDIUM, /* 100 */
194 TPM_UNDEFINED, /* 105 */
224 TPM_UNDEFINED, /* 135 */
234 TPM_UNDEFINED, /* 145 */
244 TPM_UNDEFINED, /* 155 */
254 TPM_UNDEFINED, /* 165 */
264 TPM_UNDEFINED, /* 175 */
269 TPM_MEDIUM, /* 180 */
274 TPM_MEDIUM, /* 185 */
279 TPM_UNDEFINED, /* 190 */
284 TPM_UNDEFINED, /* 195 */
299 TPM_MEDIUM, /* 210 */
304 TPM_UNDEFINED, /* 215 */
314 TPM_UNDEFINED, /* 225 */
324 TPM_UNDEFINED, /* 235 */
334 static void user_reader_timeout(unsigned long ptr)
336 struct tpm_chip *chip = (struct tpm_chip *) ptr;
338 schedule_work(&chip->work);
341 static void timeout_work(struct work_struct *work)
343 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
345 mutex_lock(&chip->buffer_mutex);
346 atomic_set(&chip->data_pending, 0);
347 memset(chip->data_buffer, 0, TPM_BUFSIZE);
348 mutex_unlock(&chip->buffer_mutex);
352 * Returns max number of jiffies to wait
354 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
357 int duration_idx = TPM_UNDEFINED;
360 if (ordinal < TPM_MAX_ORDINAL)
361 duration_idx = tpm_ordinal_duration[ordinal];
362 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
363 TPM_MAX_PROTECTED_ORDINAL)
365 tpm_protected_ordinal_duration[ordinal &
366 TPM_PROTECTED_ORDINAL_MASK];
368 if (duration_idx != TPM_UNDEFINED)
369 duration = chip->vendor.duration[duration_idx];
375 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
378 * Internal kernel interface to transmit TPM commands
380 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
387 if (bufsiz > TPM_BUFSIZE)
388 bufsiz = TPM_BUFSIZE;
390 count = be32_to_cpu(*((__be32 *) (buf + 2)));
391 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
394 if (count > bufsiz) {
396 "invalid count value %x %zx \n", count, bufsiz);
400 mutex_lock(&chip->tpm_mutex);
402 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
404 "tpm_transmit: tpm_send: error %zd\n", rc);
408 if (chip->vendor.irq)
411 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
413 u8 status = chip->vendor.status(chip);
414 if ((status & chip->vendor.req_complete_mask) ==
415 chip->vendor.req_complete_val)
418 if ((status == chip->vendor.req_canceled)) {
419 dev_err(chip->dev, "Operation Canceled\n");
424 msleep(TPM_TIMEOUT); /* CHECK */
426 } while (time_before(jiffies, stop));
428 chip->vendor.cancel(chip);
429 dev_err(chip->dev, "Operation Timed out\n");
434 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
437 "tpm_transmit: tpm_recv: error %zd\n", rc);
439 mutex_unlock(&chip->tpm_mutex);
443 #define TPM_DIGEST_SIZE 20
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 else if (len < TPM_HEADER_SIZE)
475 err = be32_to_cpu(cmd->header.out.return_code);
477 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
482 #define TPM_INTERNAL_RESULT_SIZE 200
483 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
484 #define TPM_ORD_GET_CAP cpu_to_be32(101)
486 static const struct tpm_input_header tpm_getcap_header = {
487 .tag = TPM_TAG_RQU_COMMAND,
488 .length = cpu_to_be32(22),
489 .ordinal = TPM_ORD_GET_CAP
492 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
495 struct tpm_cmd_t tpm_cmd;
497 struct tpm_chip *chip = dev_get_drvdata(dev);
499 tpm_cmd.header.in = tpm_getcap_header;
500 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
501 tpm_cmd.params.getcap_in.cap = subcap_id;
502 /*subcap field not necessary */
503 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
504 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
506 if (subcap_id == TPM_CAP_FLAG_PERM ||
507 subcap_id == TPM_CAP_FLAG_VOL)
508 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
510 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
511 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
512 tpm_cmd.params.getcap_in.subcap = subcap_id;
514 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
516 *cap = tpm_cmd.params.getcap_out.cap;
520 void tpm_gen_interrupt(struct tpm_chip *chip)
522 struct tpm_cmd_t tpm_cmd;
525 tpm_cmd.header.in = tpm_getcap_header;
526 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
527 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
528 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
530 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
531 "attempting to determine the timeouts");
533 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
535 int tpm_get_timeouts(struct tpm_chip *chip)
537 struct tpm_cmd_t tpm_cmd;
538 struct timeout_t *timeout_cap;
539 struct duration_t *duration_cap;
542 unsigned int scale = 1;
544 tpm_cmd.header.in = tpm_getcap_header;
545 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
546 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
547 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
549 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
550 "attempting to determine the timeouts");
554 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
555 be32_to_cpu(tpm_cmd.header.out.length)
556 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
559 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
560 /* Don't overwrite default if value is 0 */
561 timeout = be32_to_cpu(timeout_cap->a);
562 if (timeout && timeout < 1000) {
563 /* timeouts in msec rather usec */
565 chip->vendor.timeout_adjusted = true;
568 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
569 timeout = be32_to_cpu(timeout_cap->b);
571 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
572 timeout = be32_to_cpu(timeout_cap->c);
574 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
575 timeout = be32_to_cpu(timeout_cap->d);
577 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
580 tpm_cmd.header.in = tpm_getcap_header;
581 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
582 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
583 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
585 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
586 "attempting to determine the durations");
590 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
591 be32_to_cpu(tpm_cmd.header.out.length)
592 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
595 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
596 chip->vendor.duration[TPM_SHORT] =
597 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
598 chip->vendor.duration[TPM_MEDIUM] =
599 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
600 chip->vendor.duration[TPM_LONG] =
601 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
603 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
604 * value wrong and apparently reports msecs rather than usecs. So we
605 * fix up the resulting too-small TPM_SHORT value to make things work.
606 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
608 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
609 chip->vendor.duration[TPM_SHORT] = HZ;
610 chip->vendor.duration[TPM_MEDIUM] *= 1000;
611 chip->vendor.duration[TPM_LONG] *= 1000;
612 chip->vendor.duration_adjusted = true;
613 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
617 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
619 #define TPM_ORD_CONTINUE_SELFTEST 83
620 #define CONTINUE_SELFTEST_RESULT_SIZE 10
622 static struct tpm_input_header continue_selftest_header = {
623 .tag = TPM_TAG_RQU_COMMAND,
624 .length = cpu_to_be32(10),
625 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
629 * tpm_continue_selftest -- run TPM's selftest
630 * @chip: TPM chip to use
632 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
635 static int tpm_continue_selftest(struct tpm_chip *chip)
638 struct tpm_cmd_t cmd;
640 cmd.header.in = continue_selftest_header;
641 rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
642 "continue selftest");
646 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
652 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
653 "attempting to determine the permanent enabled state");
657 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
660 EXPORT_SYMBOL_GPL(tpm_show_enabled);
662 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
668 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
669 "attempting to determine the permanent active state");
673 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
676 EXPORT_SYMBOL_GPL(tpm_show_active);
678 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
684 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
685 "attempting to determine the owner state");
689 rc = sprintf(buf, "%d\n", cap.owned);
692 EXPORT_SYMBOL_GPL(tpm_show_owned);
694 ssize_t tpm_show_temp_deactivated(struct device * dev,
695 struct device_attribute * attr, char *buf)
700 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
701 "attempting to determine the temporary state");
705 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
708 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
711 * tpm_chip_find_get - return tpm_chip for given chip number
713 static struct tpm_chip *tpm_chip_find_get(int chip_num)
715 struct tpm_chip *pos, *chip = NULL;
718 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
719 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
722 if (try_module_get(pos->dev->driver->owner)) {
731 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
732 #define READ_PCR_RESULT_SIZE 30
733 static struct tpm_input_header pcrread_header = {
734 .tag = TPM_TAG_RQU_COMMAND,
735 .length = cpu_to_be32(14),
736 .ordinal = TPM_ORDINAL_PCRREAD
739 static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
742 struct tpm_cmd_t cmd;
744 cmd.header.in = pcrread_header;
745 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
746 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
747 "attempting to read a pcr value");
750 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
756 * tpm_pcr_read - read a pcr value
757 * @chip_num: tpm idx # or ANY
758 * @pcr_idx: pcr idx to retrieve
759 * @res_buf: TPM_PCR value
760 * size of res_buf is 20 bytes (or NULL if you don't care)
762 * The TPM driver should be built-in, but for whatever reason it
763 * isn't, protect against the chip disappearing, by incrementing
764 * the module usage count.
766 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
768 struct tpm_chip *chip;
771 chip = tpm_chip_find_get(chip_num);
774 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
778 EXPORT_SYMBOL_GPL(tpm_pcr_read);
781 * tpm_pcr_extend - extend pcr value with hash
782 * @chip_num: tpm idx # or AN&
783 * @pcr_idx: pcr idx to extend
784 * @hash: hash value used to extend pcr value
786 * The TPM driver should be built-in, but for whatever reason it
787 * isn't, protect against the chip disappearing, by incrementing
788 * the module usage count.
790 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
791 #define EXTEND_PCR_RESULT_SIZE 34
792 static struct tpm_input_header pcrextend_header = {
793 .tag = TPM_TAG_RQU_COMMAND,
794 .length = cpu_to_be32(34),
795 .ordinal = TPM_ORD_PCR_EXTEND
798 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
800 struct tpm_cmd_t cmd;
802 struct tpm_chip *chip;
804 chip = tpm_chip_find_get(chip_num);
808 cmd.header.in = pcrextend_header;
809 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
810 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
811 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
812 "attempting extend a PCR value");
817 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
820 * tpm_do_selftest - have the TPM continue its selftest and wait until it
821 * can receive further commands
822 * @chip: TPM chip to use
824 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
827 int tpm_do_selftest(struct tpm_chip *chip)
831 unsigned int delay_msec = 1000;
832 unsigned long duration;
833 struct tpm_cmd_t cmd;
835 duration = tpm_calc_ordinal_duration(chip,
836 TPM_ORD_CONTINUE_SELFTEST);
838 loops = jiffies_to_msecs(duration) / delay_msec;
840 rc = tpm_continue_selftest(chip);
841 /* This may fail if there was no TPM driver during a suspend/resume
842 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
848 /* Attempt to read a PCR value */
849 cmd.header.in = pcrread_header;
850 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
851 rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
853 if (rc < TPM_HEADER_SIZE)
856 rc = be32_to_cpu(cmd.header.out.return_code);
857 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
859 "TPM is disabled/deactivated (0x%X)\n", rc);
860 /* TPM is disabled and/or deactivated; driver can
861 * proceed and TPM does handle commands for
862 * suspend/resume correctly
866 if (rc != TPM_WARN_DOING_SELFTEST)
869 } while (--loops > 0);
873 EXPORT_SYMBOL_GPL(tpm_do_selftest);
875 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
877 struct tpm_chip *chip;
880 chip = tpm_chip_find_get(chip_num);
884 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
889 EXPORT_SYMBOL_GPL(tpm_send);
891 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
895 u8 digest[TPM_DIGEST_SIZE];
899 struct tpm_chip *chip = dev_get_drvdata(dev);
901 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
902 "attempting to determine the number of PCRS");
906 num_pcrs = be32_to_cpu(cap.num_pcrs);
907 for (i = 0; i < num_pcrs; i++) {
908 rc = __tpm_pcr_read(chip, i, digest);
911 str += sprintf(str, "PCR-%02d: ", i);
912 for (j = 0; j < TPM_DIGEST_SIZE; j++)
913 str += sprintf(str, "%02X ", digest[j]);
914 str += sprintf(str, "\n");
918 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
920 #define READ_PUBEK_RESULT_SIZE 314
921 #define TPM_ORD_READPUBEK cpu_to_be32(124)
922 struct tpm_input_header tpm_readpubek_header = {
923 .tag = TPM_TAG_RQU_COMMAND,
924 .length = cpu_to_be32(30),
925 .ordinal = TPM_ORD_READPUBEK
928 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
932 struct tpm_cmd_t tpm_cmd;
937 struct tpm_chip *chip = dev_get_drvdata(dev);
939 tpm_cmd.header.in = tpm_readpubek_header;
940 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
941 "attempting to read the PUBEK");
946 ignore header 10 bytes
947 algorithm 32 bits (1 == RSA )
950 parameters (RSA 12->bytes: keybit, #primes, expbit)
953 ignore checksum 20 bytes
955 data = tpm_cmd.params.readpubek_out_buffer;
958 "Algorithm: %02X %02X %02X %02X\n"
959 "Encscheme: %02X %02X\n"
960 "Sigscheme: %02X %02X\n"
961 "Parameters: %02X %02X %02X %02X "
962 "%02X %02X %02X %02X "
963 "%02X %02X %02X %02X\n"
964 "Modulus length: %d\n"
966 data[0], data[1], data[2], data[3],
969 data[12], data[13], data[14], data[15],
970 data[16], data[17], data[18], data[19],
971 data[20], data[21], data[22], data[23],
972 be32_to_cpu(*((__be32 *) (data + 24))));
974 for (i = 0; i < 256; i++) {
975 str += sprintf(str, "%02X ", data[i + 28]);
976 if ((i + 1) % 16 == 0)
977 str += sprintf(str, "\n");
983 EXPORT_SYMBOL_GPL(tpm_show_pubek);
986 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
993 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
994 "attempting to determine the manufacturer");
997 str += sprintf(str, "Manufacturer: 0x%x\n",
998 be32_to_cpu(cap.manufacturer_id));
1000 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
1001 "attempting to determine the 1.1 version");
1005 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1006 cap.tpm_version.Major, cap.tpm_version.Minor,
1007 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1010 EXPORT_SYMBOL_GPL(tpm_show_caps);
1012 ssize_t tpm_show_caps_1_2(struct device * dev,
1013 struct device_attribute * attr, char *buf)
1019 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1020 "attempting to determine the manufacturer");
1023 str += sprintf(str, "Manufacturer: 0x%x\n",
1024 be32_to_cpu(cap.manufacturer_id));
1025 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1026 "attempting to determine the 1.2 version");
1030 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1031 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1032 cap.tpm_version_1_2.revMajor,
1033 cap.tpm_version_1_2.revMinor);
1036 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1038 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1041 struct tpm_chip *chip = dev_get_drvdata(dev);
1043 if (chip->vendor.duration[TPM_LONG] == 0)
1046 return sprintf(buf, "%d %d %d [%s]\n",
1047 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1048 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1049 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1050 chip->vendor.duration_adjusted
1051 ? "adjusted" : "original");
1053 EXPORT_SYMBOL_GPL(tpm_show_durations);
1055 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1058 struct tpm_chip *chip = dev_get_drvdata(dev);
1060 return sprintf(buf, "%d %d %d %d [%s]\n",
1061 jiffies_to_usecs(chip->vendor.timeout_a),
1062 jiffies_to_usecs(chip->vendor.timeout_b),
1063 jiffies_to_usecs(chip->vendor.timeout_c),
1064 jiffies_to_usecs(chip->vendor.timeout_d),
1065 chip->vendor.timeout_adjusted
1066 ? "adjusted" : "original");
1068 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1070 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1071 const char *buf, size_t count)
1073 struct tpm_chip *chip = dev_get_drvdata(dev);
1077 chip->vendor.cancel(chip);
1080 EXPORT_SYMBOL_GPL(tpm_store_cancel);
1082 int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1083 wait_queue_head_t *queue)
1089 /* check current status */
1090 status = chip->vendor.status(chip);
1091 if ((status & mask) == mask)
1094 stop = jiffies + timeout;
1096 if (chip->vendor.irq) {
1098 timeout = stop - jiffies;
1099 if ((long)timeout <= 0)
1101 rc = wait_event_interruptible_timeout(*queue,
1102 ((chip->vendor.status(chip)
1107 if (rc == -ERESTARTSYS && freezing(current)) {
1108 clear_thread_flag(TIF_SIGPENDING);
1113 msleep(TPM_TIMEOUT);
1114 status = chip->vendor.status(chip);
1115 if ((status & mask) == mask)
1117 } while (time_before(jiffies, stop));
1121 EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1123 * Device file system interface to the TPM
1125 * It's assured that the chip will be opened just once,
1126 * by the check of is_open variable, which is protected
1129 int tpm_open(struct inode *inode, struct file *file)
1131 int minor = iminor(inode);
1132 struct tpm_chip *chip = NULL, *pos;
1135 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1136 if (pos->vendor.miscdev.minor == minor) {
1138 get_device(chip->dev);
1147 if (test_and_set_bit(0, &chip->is_open)) {
1148 dev_dbg(chip->dev, "Another process owns this TPM\n");
1149 put_device(chip->dev);
1153 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1154 if (chip->data_buffer == NULL) {
1155 clear_bit(0, &chip->is_open);
1156 put_device(chip->dev);
1160 atomic_set(&chip->data_pending, 0);
1162 file->private_data = chip;
1165 EXPORT_SYMBOL_GPL(tpm_open);
1168 * Called on file close
1170 int tpm_release(struct inode *inode, struct file *file)
1172 struct tpm_chip *chip = file->private_data;
1174 del_singleshot_timer_sync(&chip->user_read_timer);
1175 flush_work(&chip->work);
1176 file->private_data = NULL;
1177 atomic_set(&chip->data_pending, 0);
1178 kfree(chip->data_buffer);
1179 clear_bit(0, &chip->is_open);
1180 put_device(chip->dev);
1183 EXPORT_SYMBOL_GPL(tpm_release);
1185 ssize_t tpm_write(struct file *file, const char __user *buf,
1186 size_t size, loff_t *off)
1188 struct tpm_chip *chip = file->private_data;
1189 size_t in_size = size, out_size;
1191 /* cannot perform a write until the read has cleared
1192 either via tpm_read or a user_read_timer timeout */
1193 while (atomic_read(&chip->data_pending) != 0)
1194 msleep(TPM_TIMEOUT);
1196 mutex_lock(&chip->buffer_mutex);
1198 if (in_size > TPM_BUFSIZE)
1199 in_size = TPM_BUFSIZE;
1202 (chip->data_buffer, (void __user *) buf, in_size)) {
1203 mutex_unlock(&chip->buffer_mutex);
1207 /* atomic tpm command send and result receive */
1208 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1210 atomic_set(&chip->data_pending, out_size);
1211 mutex_unlock(&chip->buffer_mutex);
1213 /* Set a timeout by which the reader must come claim the result */
1214 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1218 EXPORT_SYMBOL_GPL(tpm_write);
1220 ssize_t tpm_read(struct file *file, char __user *buf,
1221 size_t size, loff_t *off)
1223 struct tpm_chip *chip = file->private_data;
1227 del_singleshot_timer_sync(&chip->user_read_timer);
1228 flush_work(&chip->work);
1229 ret_size = atomic_read(&chip->data_pending);
1230 atomic_set(&chip->data_pending, 0);
1231 if (ret_size > 0) { /* relay data */
1232 ssize_t orig_ret_size = ret_size;
1233 if (size < ret_size)
1236 mutex_lock(&chip->buffer_mutex);
1237 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1238 memset(chip->data_buffer, 0, orig_ret_size);
1242 mutex_unlock(&chip->buffer_mutex);
1247 EXPORT_SYMBOL_GPL(tpm_read);
1249 void tpm_remove_hardware(struct device *dev)
1251 struct tpm_chip *chip = dev_get_drvdata(dev);
1254 dev_err(dev, "No device data found\n");
1258 spin_lock(&driver_lock);
1259 list_del_rcu(&chip->list);
1260 spin_unlock(&driver_lock);
1263 misc_deregister(&chip->vendor.miscdev);
1264 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1265 tpm_bios_log_teardown(chip->bios_dir);
1267 /* write it this way to be explicit (chip->dev == dev) */
1268 put_device(chip->dev);
1270 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1272 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1273 #define SAVESTATE_RESULT_SIZE 10
1275 static struct tpm_input_header savestate_header = {
1276 .tag = TPM_TAG_RQU_COMMAND,
1277 .length = cpu_to_be32(10),
1278 .ordinal = TPM_ORD_SAVESTATE
1282 * We are about to suspend. Save the TPM state
1283 * so that it can be restored.
1285 int tpm_pm_suspend(struct device *dev)
1287 struct tpm_chip *chip = dev_get_drvdata(dev);
1288 struct tpm_cmd_t cmd;
1291 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1296 /* for buggy tpm, flush pcrs with extend to selected dummy */
1297 if (tpm_suspend_pcr) {
1298 cmd.header.in = pcrextend_header;
1299 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1300 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1302 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1303 "extending dummy pcr before suspend");
1306 /* now do the actual savestate */
1307 cmd.header.in = savestate_header;
1308 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1309 "sending savestate before suspend");
1312 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1315 * Resume from a power safe. The BIOS already restored
1318 int tpm_pm_resume(struct device *dev)
1320 struct tpm_chip *chip = dev_get_drvdata(dev);
1327 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1329 /* In case vendor provided release function, call it too.*/
1331 void tpm_dev_vendor_release(struct tpm_chip *chip)
1336 if (chip->vendor.release)
1337 chip->vendor.release(chip->dev);
1339 clear_bit(chip->dev_num, dev_mask);
1340 kfree(chip->vendor.miscdev.name);
1342 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1346 * Once all references to platform device are down to 0,
1347 * release all allocated structures.
1349 void tpm_dev_release(struct device *dev)
1351 struct tpm_chip *chip = dev_get_drvdata(dev);
1356 tpm_dev_vendor_release(chip);
1361 EXPORT_SYMBOL_GPL(tpm_dev_release);
1364 * Called from tpm_<specific>.c probe function only for devices
1365 * the driver has determined it should claim. Prior to calling
1366 * this function the specific probe function has called pci_enable_device
1367 * upon errant exit from this function specific probe function should call
1368 * pci_disable_device
1370 struct tpm_chip *tpm_register_hardware(struct device *dev,
1371 const struct tpm_vendor_specific *entry)
1373 #define DEVNAME_SIZE 7
1376 struct tpm_chip *chip;
1378 /* Driver specific per-device data */
1379 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1380 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1382 if (chip == NULL || devname == NULL)
1385 mutex_init(&chip->buffer_mutex);
1386 mutex_init(&chip->tpm_mutex);
1387 INIT_LIST_HEAD(&chip->list);
1389 INIT_WORK(&chip->work, timeout_work);
1391 setup_timer(&chip->user_read_timer, user_reader_timeout,
1392 (unsigned long)chip);
1394 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1396 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1398 if (chip->dev_num >= TPM_NUM_DEVICES) {
1399 dev_err(dev, "No available tpm device numbers\n");
1401 } else if (chip->dev_num == 0)
1402 chip->vendor.miscdev.minor = TPM_MINOR;
1404 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1406 set_bit(chip->dev_num, dev_mask);
1408 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1409 chip->vendor.miscdev.name = devname;
1411 chip->vendor.miscdev.parent = dev;
1412 chip->dev = get_device(dev);
1413 chip->release = dev->release;
1414 dev->release = tpm_dev_release;
1415 dev_set_drvdata(dev, chip);
1417 if (misc_register(&chip->vendor.miscdev)) {
1419 "unable to misc_register %s, minor %d\n",
1420 chip->vendor.miscdev.name,
1421 chip->vendor.miscdev.minor);
1425 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1426 misc_deregister(&chip->vendor.miscdev);
1430 chip->bios_dir = tpm_bios_log_setup(devname);
1432 /* Make chip available */
1433 spin_lock(&driver_lock);
1434 list_add_rcu(&chip->list, &tpm_chip_list);
1435 spin_unlock(&driver_lock);
1440 put_device(chip->dev);
1446 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1448 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1449 MODULE_DESCRIPTION("TPM Driver");
1450 MODULE_VERSION("2.0");
1451 MODULE_LICENSE("GPL");