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>
33 #include "tpm_eventlog.h"
42 #define TPM_MAX_ORDINAL 243
43 #define TPM_MAX_PROTECTED_ORDINAL 12
44 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
47 * Bug workaround - some TPM's don't flush the most
48 * recently changed pcr on suspend, so force the flush
49 * with an extend to the selected _unused_ non-volatile pcr.
51 static int tpm_suspend_pcr;
52 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
53 MODULE_PARM_DESC(suspend_pcr,
54 "PCR to use for dummy writes to faciltate flush on suspend.");
56 static LIST_HEAD(tpm_chip_list);
57 static DEFINE_SPINLOCK(driver_lock);
58 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
61 * Array with one entry per ordinal defining the maximum amount
62 * of time the chip could take to return the result. The ordinal
63 * designation of short, medium or long is defined in a table in
64 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
65 * values of the SHORT, MEDIUM, and LONG durations are retrieved
66 * from the chip during initialization with a call to tpm_get_timeouts.
68 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
69 TPM_UNDEFINED, /* 0 */
74 TPM_UNDEFINED, /* 5 */
83 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
84 TPM_UNDEFINED, /* 0 */
89 TPM_UNDEFINED, /* 5 */
139 TPM_UNDEFINED, /* 55 */
159 TPM_UNDEFINED, /* 75 */
169 TPM_UNDEFINED, /* 85 */
179 TPM_UNDEFINED, /* 95 */
184 TPM_MEDIUM, /* 100 */
189 TPM_UNDEFINED, /* 105 */
219 TPM_UNDEFINED, /* 135 */
229 TPM_UNDEFINED, /* 145 */
239 TPM_UNDEFINED, /* 155 */
249 TPM_UNDEFINED, /* 165 */
259 TPM_UNDEFINED, /* 175 */
264 TPM_MEDIUM, /* 180 */
269 TPM_MEDIUM, /* 185 */
274 TPM_UNDEFINED, /* 190 */
279 TPM_UNDEFINED, /* 195 */
294 TPM_MEDIUM, /* 210 */
299 TPM_UNDEFINED, /* 215 */
309 TPM_UNDEFINED, /* 225 */
319 TPM_UNDEFINED, /* 235 */
329 static void user_reader_timeout(unsigned long ptr)
331 struct tpm_chip *chip = (struct tpm_chip *) ptr;
333 schedule_work(&chip->work);
336 static void timeout_work(struct work_struct *work)
338 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
340 mutex_lock(&chip->buffer_mutex);
341 atomic_set(&chip->data_pending, 0);
342 memset(chip->data_buffer, 0, TPM_BUFSIZE);
343 mutex_unlock(&chip->buffer_mutex);
347 * Returns max number of jiffies to wait
349 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
352 int duration_idx = TPM_UNDEFINED;
355 if (ordinal < TPM_MAX_ORDINAL)
356 duration_idx = tpm_ordinal_duration[ordinal];
357 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
358 TPM_MAX_PROTECTED_ORDINAL)
360 tpm_protected_ordinal_duration[ordinal &
361 TPM_PROTECTED_ORDINAL_MASK];
363 if (duration_idx != TPM_UNDEFINED)
364 duration = chip->vendor.duration[duration_idx];
370 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
373 * Internal kernel interface to transmit TPM commands
375 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
382 if (bufsiz > TPM_BUFSIZE)
383 bufsiz = TPM_BUFSIZE;
385 count = be32_to_cpu(*((__be32 *) (buf + 2)));
386 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
389 if (count > bufsiz) {
391 "invalid count value %x %zx \n", count, bufsiz);
395 mutex_lock(&chip->tpm_mutex);
397 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
399 "tpm_transmit: tpm_send: error %zd\n", rc);
403 if (chip->vendor.irq)
406 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
408 u8 status = chip->vendor.status(chip);
409 if ((status & chip->vendor.req_complete_mask) ==
410 chip->vendor.req_complete_val)
413 if ((status == chip->vendor.req_canceled)) {
414 dev_err(chip->dev, "Operation Canceled\n");
419 msleep(TPM_TIMEOUT); /* CHECK */
421 } while (time_before(jiffies, stop));
423 chip->vendor.cancel(chip);
424 dev_err(chip->dev, "Operation Timed out\n");
429 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
432 "tpm_transmit: tpm_recv: error %zd\n", rc);
434 mutex_unlock(&chip->tpm_mutex);
438 #define TPM_DIGEST_SIZE 20
439 #define TPM_RET_CODE_IDX 6
441 enum tpm_capabilities {
442 TPM_CAP_FLAG = cpu_to_be32(4),
443 TPM_CAP_PROP = cpu_to_be32(5),
444 CAP_VERSION_1_1 = cpu_to_be32(0x06),
445 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
448 enum tpm_sub_capabilities {
449 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
450 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
451 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
452 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
453 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
454 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
455 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
459 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
460 int len, const char *desc)
464 len = tpm_transmit(chip,(u8 *) cmd, len);
467 else if (len < TPM_HEADER_SIZE)
470 err = be32_to_cpu(cmd->header.out.return_code);
472 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
477 #define TPM_INTERNAL_RESULT_SIZE 200
478 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
479 #define TPM_ORD_GET_CAP cpu_to_be32(101)
480 #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
482 static const struct tpm_input_header tpm_getcap_header = {
483 .tag = TPM_TAG_RQU_COMMAND,
484 .length = cpu_to_be32(22),
485 .ordinal = TPM_ORD_GET_CAP
488 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
491 struct tpm_cmd_t tpm_cmd;
493 struct tpm_chip *chip = dev_get_drvdata(dev);
495 tpm_cmd.header.in = tpm_getcap_header;
496 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
497 tpm_cmd.params.getcap_in.cap = subcap_id;
498 /*subcap field not necessary */
499 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
500 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
502 if (subcap_id == TPM_CAP_FLAG_PERM ||
503 subcap_id == TPM_CAP_FLAG_VOL)
504 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
506 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
507 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
508 tpm_cmd.params.getcap_in.subcap = subcap_id;
510 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
512 *cap = tpm_cmd.params.getcap_out.cap;
516 void tpm_gen_interrupt(struct tpm_chip *chip)
518 struct tpm_cmd_t tpm_cmd;
521 tpm_cmd.header.in = tpm_getcap_header;
522 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
523 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
524 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
526 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
527 "attempting to determine the timeouts");
529 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
531 int tpm_get_timeouts(struct tpm_chip *chip)
533 struct tpm_cmd_t tpm_cmd;
534 struct timeout_t *timeout_cap;
535 struct duration_t *duration_cap;
538 unsigned int scale = 1;
540 tpm_cmd.header.in = tpm_getcap_header;
541 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
542 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
543 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
545 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
546 "attempting to determine the timeouts");
550 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
551 be32_to_cpu(tpm_cmd.header.out.length)
552 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
555 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
556 /* Don't overwrite default if value is 0 */
557 timeout = be32_to_cpu(timeout_cap->a);
558 if (timeout && timeout < 1000) {
559 /* timeouts in msec rather usec */
561 chip->vendor.timeout_adjusted = true;
564 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
565 timeout = be32_to_cpu(timeout_cap->b);
567 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
568 timeout = be32_to_cpu(timeout_cap->c);
570 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
571 timeout = be32_to_cpu(timeout_cap->d);
573 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
576 tpm_cmd.header.in = tpm_getcap_header;
577 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
578 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
579 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
581 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
582 "attempting to determine the durations");
586 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
587 be32_to_cpu(tpm_cmd.header.out.length)
588 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
591 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
592 chip->vendor.duration[TPM_SHORT] =
593 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
594 chip->vendor.duration[TPM_MEDIUM] =
595 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
596 chip->vendor.duration[TPM_LONG] =
597 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
599 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
600 * value wrong and apparently reports msecs rather than usecs. So we
601 * fix up the resulting too-small TPM_SHORT value to make things work.
602 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
604 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
605 chip->vendor.duration[TPM_SHORT] = HZ;
606 chip->vendor.duration[TPM_MEDIUM] *= 1000;
607 chip->vendor.duration[TPM_LONG] *= 1000;
608 chip->vendor.duration_adjusted = true;
609 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
613 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
615 #define TPM_ORD_CONTINUE_SELFTEST 83
616 #define CONTINUE_SELFTEST_RESULT_SIZE 10
618 static struct tpm_input_header continue_selftest_header = {
619 .tag = TPM_TAG_RQU_COMMAND,
620 .length = cpu_to_be32(10),
621 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
625 * tpm_continue_selftest -- run TPM's selftest
626 * @chip: TPM chip to use
628 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
631 static int tpm_continue_selftest(struct tpm_chip *chip)
634 struct tpm_cmd_t cmd;
636 cmd.header.in = continue_selftest_header;
637 rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
638 "continue selftest");
642 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
648 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
649 "attempting to determine the permanent enabled state");
653 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
656 EXPORT_SYMBOL_GPL(tpm_show_enabled);
658 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
664 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
665 "attempting to determine the permanent active state");
669 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
672 EXPORT_SYMBOL_GPL(tpm_show_active);
674 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
680 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
681 "attempting to determine the owner state");
685 rc = sprintf(buf, "%d\n", cap.owned);
688 EXPORT_SYMBOL_GPL(tpm_show_owned);
690 ssize_t tpm_show_temp_deactivated(struct device * dev,
691 struct device_attribute * attr, char *buf)
696 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
697 "attempting to determine the temporary state");
701 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
704 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
707 * tpm_chip_find_get - return tpm_chip for given chip number
709 static struct tpm_chip *tpm_chip_find_get(int chip_num)
711 struct tpm_chip *pos, *chip = NULL;
714 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
715 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
718 if (try_module_get(pos->dev->driver->owner)) {
727 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
728 #define READ_PCR_RESULT_SIZE 30
729 static struct tpm_input_header pcrread_header = {
730 .tag = TPM_TAG_RQU_COMMAND,
731 .length = cpu_to_be32(14),
732 .ordinal = TPM_ORDINAL_PCRREAD
735 static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
738 struct tpm_cmd_t cmd;
740 cmd.header.in = pcrread_header;
741 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
742 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
743 "attempting to read a pcr value");
746 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
752 * tpm_pcr_read - read a pcr value
753 * @chip_num: tpm idx # or ANY
754 * @pcr_idx: pcr idx to retrieve
755 * @res_buf: TPM_PCR value
756 * size of res_buf is 20 bytes (or NULL if you don't care)
758 * The TPM driver should be built-in, but for whatever reason it
759 * isn't, protect against the chip disappearing, by incrementing
760 * the module usage count.
762 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
764 struct tpm_chip *chip;
767 chip = tpm_chip_find_get(chip_num);
770 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
774 EXPORT_SYMBOL_GPL(tpm_pcr_read);
777 * tpm_pcr_extend - extend pcr value with hash
778 * @chip_num: tpm idx # or AN&
779 * @pcr_idx: pcr idx to extend
780 * @hash: hash value used to extend pcr value
782 * The TPM driver should be built-in, but for whatever reason it
783 * isn't, protect against the chip disappearing, by incrementing
784 * the module usage count.
786 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
787 #define EXTEND_PCR_RESULT_SIZE 34
788 static struct tpm_input_header pcrextend_header = {
789 .tag = TPM_TAG_RQU_COMMAND,
790 .length = cpu_to_be32(34),
791 .ordinal = TPM_ORD_PCR_EXTEND
794 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
796 struct tpm_cmd_t cmd;
798 struct tpm_chip *chip;
800 chip = tpm_chip_find_get(chip_num);
804 cmd.header.in = pcrextend_header;
805 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
806 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
807 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
808 "attempting extend a PCR value");
813 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
816 * tpm_do_selftest - have the TPM continue its selftest and wait until it
817 * can receive further commands
818 * @chip: TPM chip to use
820 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
823 int tpm_do_selftest(struct tpm_chip *chip)
827 unsigned int delay_msec = 1000;
828 unsigned long duration;
829 struct tpm_cmd_t cmd;
831 duration = tpm_calc_ordinal_duration(chip,
832 TPM_ORD_CONTINUE_SELFTEST);
834 loops = jiffies_to_msecs(duration) / delay_msec;
836 rc = tpm_continue_selftest(chip);
837 /* This may fail if there was no TPM driver during a suspend/resume
838 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
844 /* Attempt to read a PCR value */
845 cmd.header.in = pcrread_header;
846 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
847 rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
849 if (rc < TPM_HEADER_SIZE)
852 rc = be32_to_cpu(cmd.header.out.return_code);
853 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
855 "TPM is disabled/deactivated (0x%X)\n", rc);
856 /* TPM is disabled and/or deactivated; driver can
857 * proceed and TPM does handle commands for
858 * suspend/resume correctly
862 if (rc != TPM_WARN_DOING_SELFTEST)
865 } while (--loops > 0);
869 EXPORT_SYMBOL_GPL(tpm_do_selftest);
871 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
873 struct tpm_chip *chip;
876 chip = tpm_chip_find_get(chip_num);
880 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
885 EXPORT_SYMBOL_GPL(tpm_send);
887 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
891 u8 digest[TPM_DIGEST_SIZE];
895 struct tpm_chip *chip = dev_get_drvdata(dev);
897 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
898 "attempting to determine the number of PCRS");
902 num_pcrs = be32_to_cpu(cap.num_pcrs);
903 for (i = 0; i < num_pcrs; i++) {
904 rc = __tpm_pcr_read(chip, i, digest);
907 str += sprintf(str, "PCR-%02d: ", i);
908 for (j = 0; j < TPM_DIGEST_SIZE; j++)
909 str += sprintf(str, "%02X ", digest[j]);
910 str += sprintf(str, "\n");
914 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
916 #define READ_PUBEK_RESULT_SIZE 314
917 #define TPM_ORD_READPUBEK cpu_to_be32(124)
918 struct tpm_input_header tpm_readpubek_header = {
919 .tag = TPM_TAG_RQU_COMMAND,
920 .length = cpu_to_be32(30),
921 .ordinal = TPM_ORD_READPUBEK
924 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
928 struct tpm_cmd_t tpm_cmd;
933 struct tpm_chip *chip = dev_get_drvdata(dev);
935 tpm_cmd.header.in = tpm_readpubek_header;
936 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
937 "attempting to read the PUBEK");
942 ignore header 10 bytes
943 algorithm 32 bits (1 == RSA )
946 parameters (RSA 12->bytes: keybit, #primes, expbit)
949 ignore checksum 20 bytes
951 data = tpm_cmd.params.readpubek_out_buffer;
954 "Algorithm: %02X %02X %02X %02X\n"
955 "Encscheme: %02X %02X\n"
956 "Sigscheme: %02X %02X\n"
957 "Parameters: %02X %02X %02X %02X "
958 "%02X %02X %02X %02X "
959 "%02X %02X %02X %02X\n"
960 "Modulus length: %d\n"
962 data[0], data[1], data[2], data[3],
965 data[12], data[13], data[14], data[15],
966 data[16], data[17], data[18], data[19],
967 data[20], data[21], data[22], data[23],
968 be32_to_cpu(*((__be32 *) (data + 24))));
970 for (i = 0; i < 256; i++) {
971 str += sprintf(str, "%02X ", data[i + 28]);
972 if ((i + 1) % 16 == 0)
973 str += sprintf(str, "\n");
979 EXPORT_SYMBOL_GPL(tpm_show_pubek);
982 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
989 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
990 "attempting to determine the manufacturer");
993 str += sprintf(str, "Manufacturer: 0x%x\n",
994 be32_to_cpu(cap.manufacturer_id));
996 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
997 "attempting to determine the 1.1 version");
1001 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1002 cap.tpm_version.Major, cap.tpm_version.Minor,
1003 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1006 EXPORT_SYMBOL_GPL(tpm_show_caps);
1008 ssize_t tpm_show_caps_1_2(struct device * dev,
1009 struct device_attribute * attr, char *buf)
1015 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1016 "attempting to determine the manufacturer");
1019 str += sprintf(str, "Manufacturer: 0x%x\n",
1020 be32_to_cpu(cap.manufacturer_id));
1021 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1022 "attempting to determine the 1.2 version");
1026 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1027 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1028 cap.tpm_version_1_2.revMajor,
1029 cap.tpm_version_1_2.revMinor);
1032 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1034 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1037 struct tpm_chip *chip = dev_get_drvdata(dev);
1039 if (chip->vendor.duration[TPM_LONG] == 0)
1042 return sprintf(buf, "%d %d %d [%s]\n",
1043 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1044 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1045 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1046 chip->vendor.duration_adjusted
1047 ? "adjusted" : "original");
1049 EXPORT_SYMBOL_GPL(tpm_show_durations);
1051 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1054 struct tpm_chip *chip = dev_get_drvdata(dev);
1056 return sprintf(buf, "%d %d %d %d [%s]\n",
1057 jiffies_to_usecs(chip->vendor.timeout_a),
1058 jiffies_to_usecs(chip->vendor.timeout_b),
1059 jiffies_to_usecs(chip->vendor.timeout_c),
1060 jiffies_to_usecs(chip->vendor.timeout_d),
1061 chip->vendor.timeout_adjusted
1062 ? "adjusted" : "original");
1064 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1066 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1067 const char *buf, size_t count)
1069 struct tpm_chip *chip = dev_get_drvdata(dev);
1073 chip->vendor.cancel(chip);
1076 EXPORT_SYMBOL_GPL(tpm_store_cancel);
1078 int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1079 wait_queue_head_t *queue)
1085 /* check current status */
1086 status = chip->vendor.status(chip);
1087 if ((status & mask) == mask)
1090 stop = jiffies + timeout;
1092 if (chip->vendor.irq) {
1094 timeout = stop - jiffies;
1095 if ((long)timeout <= 0)
1097 rc = wait_event_interruptible_timeout(*queue,
1098 ((chip->vendor.status(chip)
1103 if (rc == -ERESTARTSYS && freezing(current)) {
1104 clear_thread_flag(TIF_SIGPENDING);
1109 msleep(TPM_TIMEOUT);
1110 status = chip->vendor.status(chip);
1111 if ((status & mask) == mask)
1113 } while (time_before(jiffies, stop));
1117 EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1119 * Device file system interface to the TPM
1121 * It's assured that the chip will be opened just once,
1122 * by the check of is_open variable, which is protected
1125 int tpm_open(struct inode *inode, struct file *file)
1127 int minor = iminor(inode);
1128 struct tpm_chip *chip = NULL, *pos;
1131 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1132 if (pos->vendor.miscdev.minor == minor) {
1134 get_device(chip->dev);
1143 if (test_and_set_bit(0, &chip->is_open)) {
1144 dev_dbg(chip->dev, "Another process owns this TPM\n");
1145 put_device(chip->dev);
1149 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1150 if (chip->data_buffer == NULL) {
1151 clear_bit(0, &chip->is_open);
1152 put_device(chip->dev);
1156 atomic_set(&chip->data_pending, 0);
1158 file->private_data = chip;
1161 EXPORT_SYMBOL_GPL(tpm_open);
1164 * Called on file close
1166 int tpm_release(struct inode *inode, struct file *file)
1168 struct tpm_chip *chip = file->private_data;
1170 del_singleshot_timer_sync(&chip->user_read_timer);
1171 flush_work_sync(&chip->work);
1172 file->private_data = NULL;
1173 atomic_set(&chip->data_pending, 0);
1174 kzfree(chip->data_buffer);
1175 clear_bit(0, &chip->is_open);
1176 put_device(chip->dev);
1179 EXPORT_SYMBOL_GPL(tpm_release);
1181 ssize_t tpm_write(struct file *file, const char __user *buf,
1182 size_t size, loff_t *off)
1184 struct tpm_chip *chip = file->private_data;
1185 size_t in_size = size, out_size;
1187 /* cannot perform a write until the read has cleared
1188 either via tpm_read or a user_read_timer timeout */
1189 while (atomic_read(&chip->data_pending) != 0)
1190 msleep(TPM_TIMEOUT);
1192 mutex_lock(&chip->buffer_mutex);
1194 if (in_size > TPM_BUFSIZE)
1195 in_size = TPM_BUFSIZE;
1198 (chip->data_buffer, (void __user *) buf, in_size)) {
1199 mutex_unlock(&chip->buffer_mutex);
1203 /* atomic tpm command send and result receive */
1204 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1206 atomic_set(&chip->data_pending, out_size);
1207 mutex_unlock(&chip->buffer_mutex);
1209 /* Set a timeout by which the reader must come claim the result */
1210 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1214 EXPORT_SYMBOL_GPL(tpm_write);
1216 ssize_t tpm_read(struct file *file, char __user *buf,
1217 size_t size, loff_t *off)
1219 struct tpm_chip *chip = file->private_data;
1223 del_singleshot_timer_sync(&chip->user_read_timer);
1224 flush_work_sync(&chip->work);
1225 ret_size = atomic_read(&chip->data_pending);
1226 if (ret_size > 0) { /* relay data */
1227 ssize_t orig_ret_size = ret_size;
1228 if (size < ret_size)
1231 mutex_lock(&chip->buffer_mutex);
1232 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1233 memset(chip->data_buffer, 0, orig_ret_size);
1237 mutex_unlock(&chip->buffer_mutex);
1240 atomic_set(&chip->data_pending, 0);
1244 EXPORT_SYMBOL_GPL(tpm_read);
1246 void tpm_remove_hardware(struct device *dev)
1248 struct tpm_chip *chip = dev_get_drvdata(dev);
1251 dev_err(dev, "No device data found\n");
1255 spin_lock(&driver_lock);
1256 list_del_rcu(&chip->list);
1257 spin_unlock(&driver_lock);
1260 misc_deregister(&chip->vendor.miscdev);
1261 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1262 tpm_bios_log_teardown(chip->bios_dir);
1264 /* write it this way to be explicit (chip->dev == dev) */
1265 put_device(chip->dev);
1267 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1269 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1270 #define SAVESTATE_RESULT_SIZE 10
1272 static struct tpm_input_header savestate_header = {
1273 .tag = TPM_TAG_RQU_COMMAND,
1274 .length = cpu_to_be32(10),
1275 .ordinal = TPM_ORD_SAVESTATE
1279 * We are about to suspend. Save the TPM state
1280 * so that it can be restored.
1282 int tpm_pm_suspend(struct device *dev)
1284 struct tpm_chip *chip = dev_get_drvdata(dev);
1285 struct tpm_cmd_t cmd;
1288 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1293 /* for buggy tpm, flush pcrs with extend to selected dummy */
1294 if (tpm_suspend_pcr) {
1295 cmd.header.in = pcrextend_header;
1296 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1297 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1299 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1300 "extending dummy pcr before suspend");
1303 /* now do the actual savestate */
1304 cmd.header.in = savestate_header;
1305 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1306 "sending savestate before suspend");
1309 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1312 * Resume from a power safe. The BIOS already restored
1315 int tpm_pm_resume(struct device *dev)
1317 struct tpm_chip *chip = dev_get_drvdata(dev);
1324 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1326 #define TPM_GETRANDOM_RESULT_SIZE 18
1327 static struct tpm_input_header tpm_getrandom_header = {
1328 .tag = TPM_TAG_RQU_COMMAND,
1329 .length = cpu_to_be32(14),
1330 .ordinal = TPM_ORD_GET_RANDOM
1334 * tpm_get_random() - Get random bytes from the tpm's RNG
1335 * @chip_num: A specific chip number for the request or TPM_ANY_NUM
1336 * @out: destination buffer for the random bytes
1337 * @max: the max number of bytes to write to @out
1339 * Returns < 0 on error and the number of bytes read on success
1341 int tpm_get_random(u32 chip_num, u8 *out, size_t max)
1343 struct tpm_chip *chip;
1344 struct tpm_cmd_t tpm_cmd;
1345 u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
1346 int err, total = 0, retries = 5;
1349 chip = tpm_chip_find_get(chip_num);
1353 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
1357 tpm_cmd.header.in = tpm_getrandom_header;
1358 tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
1360 err = transmit_cmd(chip, &tpm_cmd,
1361 TPM_GETRANDOM_RESULT_SIZE + num_bytes,
1362 "attempting get random");
1366 recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
1367 memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
1372 } while (retries-- && total < max);
1374 return total ? total : -EIO;
1376 EXPORT_SYMBOL_GPL(tpm_get_random);
1378 /* In case vendor provided release function, call it too.*/
1380 void tpm_dev_vendor_release(struct tpm_chip *chip)
1385 if (chip->vendor.release)
1386 chip->vendor.release(chip->dev);
1388 clear_bit(chip->dev_num, dev_mask);
1389 kfree(chip->vendor.miscdev.name);
1391 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1395 * Once all references to platform device are down to 0,
1396 * release all allocated structures.
1398 void tpm_dev_release(struct device *dev)
1400 struct tpm_chip *chip = dev_get_drvdata(dev);
1405 tpm_dev_vendor_release(chip);
1410 EXPORT_SYMBOL_GPL(tpm_dev_release);
1413 * Called from tpm_<specific>.c probe function only for devices
1414 * the driver has determined it should claim. Prior to calling
1415 * this function the specific probe function has called pci_enable_device
1416 * upon errant exit from this function specific probe function should call
1417 * pci_disable_device
1419 struct tpm_chip *tpm_register_hardware(struct device *dev,
1420 const struct tpm_vendor_specific *entry)
1422 #define DEVNAME_SIZE 7
1425 struct tpm_chip *chip;
1427 /* Driver specific per-device data */
1428 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1429 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1431 if (chip == NULL || devname == NULL)
1434 mutex_init(&chip->buffer_mutex);
1435 mutex_init(&chip->tpm_mutex);
1436 INIT_LIST_HEAD(&chip->list);
1438 INIT_WORK(&chip->work, timeout_work);
1440 setup_timer(&chip->user_read_timer, user_reader_timeout,
1441 (unsigned long)chip);
1443 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1445 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1447 if (chip->dev_num >= TPM_NUM_DEVICES) {
1448 dev_err(dev, "No available tpm device numbers\n");
1450 } else if (chip->dev_num == 0)
1451 chip->vendor.miscdev.minor = TPM_MINOR;
1453 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1455 set_bit(chip->dev_num, dev_mask);
1457 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1458 chip->vendor.miscdev.name = devname;
1460 chip->vendor.miscdev.parent = dev;
1461 chip->dev = get_device(dev);
1462 chip->release = dev->release;
1463 dev->release = tpm_dev_release;
1464 dev_set_drvdata(dev, chip);
1466 if (misc_register(&chip->vendor.miscdev)) {
1468 "unable to misc_register %s, minor %d\n",
1469 chip->vendor.miscdev.name,
1470 chip->vendor.miscdev.minor);
1474 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1475 misc_deregister(&chip->vendor.miscdev);
1479 if (sys_add_ppi(&dev->kobj)) {
1480 misc_deregister(&chip->vendor.miscdev);
1484 chip->bios_dir = tpm_bios_log_setup(devname);
1486 /* Make chip available */
1487 spin_lock(&driver_lock);
1488 list_add_rcu(&chip->list, &tpm_chip_list);
1489 spin_unlock(&driver_lock);
1494 put_device(chip->dev);
1500 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1502 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1503 MODULE_DESCRIPTION("TPM Driver");
1504 MODULE_VERSION("2.0");
1505 MODULE_LICENSE("GPL");