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 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
387 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
390 if (count
> bufsiz
) {
392 "invalid count value %x %zx \n", count
, bufsiz
);
396 mutex_lock(&chip
->tpm_mutex
);
398 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
400 "tpm_transmit: tpm_send: error %zd\n", rc
);
404 if (chip
->vendor
.irq
)
407 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
409 u8 status
= chip
->vendor
.status(chip
);
410 if ((status
& chip
->vendor
.req_complete_mask
) ==
411 chip
->vendor
.req_complete_val
)
414 if ((status
== chip
->vendor
.req_canceled
)) {
415 dev_err(chip
->dev
, "Operation Canceled\n");
420 msleep(TPM_TIMEOUT
); /* CHECK */
422 } while (time_before(jiffies
, stop
));
424 chip
->vendor
.cancel(chip
);
425 dev_err(chip
->dev
, "Operation Timed out\n");
430 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
433 "tpm_transmit: tpm_recv: error %zd\n", rc
);
435 mutex_unlock(&chip
->tpm_mutex
);
439 #define TPM_DIGEST_SIZE 20
440 #define TPM_ERROR_SIZE 10
441 #define TPM_RET_CODE_IDX 6
443 enum tpm_capabilities
{
444 TPM_CAP_FLAG
= cpu_to_be32(4),
445 TPM_CAP_PROP
= cpu_to_be32(5),
446 CAP_VERSION_1_1
= cpu_to_be32(0x06),
447 CAP_VERSION_1_2
= cpu_to_be32(0x1A)
450 enum tpm_sub_capabilities
{
451 TPM_CAP_PROP_PCR
= cpu_to_be32(0x101),
452 TPM_CAP_PROP_MANUFACTURER
= cpu_to_be32(0x103),
453 TPM_CAP_FLAG_PERM
= cpu_to_be32(0x108),
454 TPM_CAP_FLAG_VOL
= cpu_to_be32(0x109),
455 TPM_CAP_PROP_OWNER
= cpu_to_be32(0x111),
456 TPM_CAP_PROP_TIS_TIMEOUT
= cpu_to_be32(0x115),
457 TPM_CAP_PROP_TIS_DURATION
= cpu_to_be32(0x120),
461 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, struct tpm_cmd_t
*cmd
,
462 int len
, const char *desc
)
466 len
= tpm_transmit(chip
,(u8
*) cmd
, len
);
469 if (len
== TPM_ERROR_SIZE
) {
470 err
= be32_to_cpu(cmd
->header
.out
.return_code
);
471 dev_dbg(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)
481 static const struct tpm_input_header tpm_getcap_header
= {
482 .tag
= TPM_TAG_RQU_COMMAND
,
483 .length
= cpu_to_be32(22),
484 .ordinal
= TPM_ORD_GET_CAP
487 ssize_t
tpm_getcap(struct device
*dev
, __be32 subcap_id
, cap_t
*cap
,
490 struct tpm_cmd_t tpm_cmd
;
492 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
494 tpm_cmd
.header
.in
= tpm_getcap_header
;
495 if (subcap_id
== CAP_VERSION_1_1
|| subcap_id
== CAP_VERSION_1_2
) {
496 tpm_cmd
.params
.getcap_in
.cap
= subcap_id
;
497 /*subcap field not necessary */
498 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(0);
499 tpm_cmd
.header
.in
.length
-= cpu_to_be32(sizeof(__be32
));
501 if (subcap_id
== TPM_CAP_FLAG_PERM
||
502 subcap_id
== TPM_CAP_FLAG_VOL
)
503 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_FLAG
;
505 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
506 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
507 tpm_cmd
.params
.getcap_in
.subcap
= subcap_id
;
509 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
, desc
);
511 *cap
= tpm_cmd
.params
.getcap_out
.cap
;
515 void tpm_gen_interrupt(struct tpm_chip
*chip
)
517 struct tpm_cmd_t tpm_cmd
;
520 tpm_cmd
.header
.in
= tpm_getcap_header
;
521 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
522 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
523 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
525 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
526 "attempting to determine the timeouts");
528 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
530 void tpm_get_timeouts(struct tpm_chip
*chip
)
532 struct tpm_cmd_t tpm_cmd
;
533 struct timeout_t
*timeout_cap
;
534 struct duration_t
*duration_cap
;
537 unsigned int scale
= 1;
539 tpm_cmd
.header
.in
= tpm_getcap_header
;
540 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
541 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
542 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
544 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
545 "attempting to determine the timeouts");
549 if (be32_to_cpu(tpm_cmd
.header
.out
.return_code
) != 0 ||
550 be32_to_cpu(tpm_cmd
.header
.out
.length
)
551 != sizeof(tpm_cmd
.header
.out
) + sizeof(u32
) + 4 * sizeof(u32
))
554 timeout_cap
= &tpm_cmd
.params
.getcap_out
.cap
.timeout
;
555 /* Don't overwrite default if value is 0 */
556 timeout
= be32_to_cpu(timeout_cap
->a
);
557 if (timeout
&& timeout
< 1000) {
558 /* timeouts in msec rather usec */
560 chip
->vendor
.timeout_adjusted
= true;
563 chip
->vendor
.timeout_a
= usecs_to_jiffies(timeout
* scale
);
564 timeout
= be32_to_cpu(timeout_cap
->b
);
566 chip
->vendor
.timeout_b
= usecs_to_jiffies(timeout
* scale
);
567 timeout
= be32_to_cpu(timeout_cap
->c
);
569 chip
->vendor
.timeout_c
= usecs_to_jiffies(timeout
* scale
);
570 timeout
= be32_to_cpu(timeout_cap
->d
);
572 chip
->vendor
.timeout_d
= usecs_to_jiffies(timeout
* scale
);
575 tpm_cmd
.header
.in
= tpm_getcap_header
;
576 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
577 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
578 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_DURATION
;
580 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
581 "attempting to determine the durations");
585 if (be32_to_cpu(tpm_cmd
.header
.out
.return_code
) != 0 ||
586 be32_to_cpu(tpm_cmd
.header
.out
.length
)
587 != sizeof(tpm_cmd
.header
.out
) + sizeof(u32
) + 3 * sizeof(u32
))
590 duration_cap
= &tpm_cmd
.params
.getcap_out
.cap
.duration
;
591 chip
->vendor
.duration
[TPM_SHORT
] =
592 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_short
));
593 chip
->vendor
.duration
[TPM_MEDIUM
] =
594 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_medium
));
595 chip
->vendor
.duration
[TPM_LONG
] =
596 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_long
));
598 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
599 * value wrong and apparently reports msecs rather than usecs. So we
600 * fix up the resulting too-small TPM_SHORT value to make things work.
601 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
603 if (chip
->vendor
.duration
[TPM_SHORT
] < (HZ
/ 100)) {
604 chip
->vendor
.duration
[TPM_SHORT
] = HZ
;
605 chip
->vendor
.duration
[TPM_MEDIUM
] *= 1000;
606 chip
->vendor
.duration
[TPM_LONG
] *= 1000;
607 chip
->vendor
.duration_adjusted
= true;
608 dev_info(chip
->dev
, "Adjusting TPM timeout parameters.");
611 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
613 void tpm_continue_selftest(struct tpm_chip
*chip
)
616 0, 193, /* TPM_TAG_RQU_COMMAND */
617 0, 0, 0, 10, /* length */
618 0, 0, 0, 83, /* TPM_ORD_ContinueSelfTest */
621 tpm_transmit(chip
, data
, sizeof(data
));
623 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
625 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
631 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
632 "attempting to determine the permanent enabled state");
636 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.disable
);
639 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
641 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
647 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
648 "attempting to determine the permanent active state");
652 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.deactivated
);
655 EXPORT_SYMBOL_GPL(tpm_show_active
);
657 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
663 rc
= tpm_getcap(dev
, TPM_CAP_PROP_OWNER
, &cap
,
664 "attempting to determine the owner state");
668 rc
= sprintf(buf
, "%d\n", cap
.owned
);
671 EXPORT_SYMBOL_GPL(tpm_show_owned
);
673 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
674 struct device_attribute
* attr
, char *buf
)
679 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_VOL
, &cap
,
680 "attempting to determine the temporary state");
684 rc
= sprintf(buf
, "%d\n", cap
.stclear_flags
.deactivated
);
687 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
690 * tpm_chip_find_get - return tpm_chip for given chip number
692 static struct tpm_chip
*tpm_chip_find_get(int chip_num
)
694 struct tpm_chip
*pos
, *chip
= NULL
;
697 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
698 if (chip_num
!= TPM_ANY_NUM
&& chip_num
!= pos
->dev_num
)
701 if (try_module_get(pos
->dev
->driver
->owner
)) {
710 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
711 #define READ_PCR_RESULT_SIZE 30
712 static struct tpm_input_header pcrread_header
= {
713 .tag
= TPM_TAG_RQU_COMMAND
,
714 .length
= cpu_to_be32(14),
715 .ordinal
= TPM_ORDINAL_PCRREAD
718 int __tpm_pcr_read(struct tpm_chip
*chip
, int pcr_idx
, u8
*res_buf
)
721 struct tpm_cmd_t cmd
;
723 cmd
.header
.in
= pcrread_header
;
724 cmd
.params
.pcrread_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
725 rc
= transmit_cmd(chip
, &cmd
, READ_PCR_RESULT_SIZE
,
726 "attempting to read a pcr value");
729 memcpy(res_buf
, cmd
.params
.pcrread_out
.pcr_result
,
735 * tpm_pcr_read - read a pcr value
736 * @chip_num: tpm idx # or ANY
737 * @pcr_idx: pcr idx to retrieve
738 * @res_buf: TPM_PCR value
739 * size of res_buf is 20 bytes (or NULL if you don't care)
741 * The TPM driver should be built-in, but for whatever reason it
742 * isn't, protect against the chip disappearing, by incrementing
743 * the module usage count.
745 int tpm_pcr_read(u32 chip_num
, int pcr_idx
, u8
*res_buf
)
747 struct tpm_chip
*chip
;
750 chip
= tpm_chip_find_get(chip_num
);
753 rc
= __tpm_pcr_read(chip
, pcr_idx
, res_buf
);
757 EXPORT_SYMBOL_GPL(tpm_pcr_read
);
760 * tpm_pcr_extend - extend pcr value with hash
761 * @chip_num: tpm idx # or AN&
762 * @pcr_idx: pcr idx to extend
763 * @hash: hash value used to extend pcr value
765 * The TPM driver should be built-in, but for whatever reason it
766 * isn't, protect against the chip disappearing, by incrementing
767 * the module usage count.
769 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
770 #define EXTEND_PCR_RESULT_SIZE 34
771 static struct tpm_input_header pcrextend_header
= {
772 .tag
= TPM_TAG_RQU_COMMAND
,
773 .length
= cpu_to_be32(34),
774 .ordinal
= TPM_ORD_PCR_EXTEND
777 int tpm_pcr_extend(u32 chip_num
, int pcr_idx
, const u8
*hash
)
779 struct tpm_cmd_t cmd
;
781 struct tpm_chip
*chip
;
783 chip
= tpm_chip_find_get(chip_num
);
787 cmd
.header
.in
= pcrextend_header
;
788 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
789 memcpy(cmd
.params
.pcrextend_in
.hash
, hash
, TPM_DIGEST_SIZE
);
790 rc
= transmit_cmd(chip
, &cmd
, EXTEND_PCR_RESULT_SIZE
,
791 "attempting extend a PCR value");
796 EXPORT_SYMBOL_GPL(tpm_pcr_extend
);
798 int tpm_send(u32 chip_num
, void *cmd
, size_t buflen
)
800 struct tpm_chip
*chip
;
803 chip
= tpm_chip_find_get(chip_num
);
807 rc
= transmit_cmd(chip
, cmd
, buflen
, "attempting tpm_cmd");
812 EXPORT_SYMBOL_GPL(tpm_send
);
814 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
818 u8 digest
[TPM_DIGEST_SIZE
];
822 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
824 rc
= tpm_getcap(dev
, TPM_CAP_PROP_PCR
, &cap
,
825 "attempting to determine the number of PCRS");
829 num_pcrs
= be32_to_cpu(cap
.num_pcrs
);
830 for (i
= 0; i
< num_pcrs
; i
++) {
831 rc
= __tpm_pcr_read(chip
, i
, digest
);
834 str
+= sprintf(str
, "PCR-%02d: ", i
);
835 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
836 str
+= sprintf(str
, "%02X ", digest
[j
]);
837 str
+= sprintf(str
, "\n");
841 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
843 #define READ_PUBEK_RESULT_SIZE 314
844 #define TPM_ORD_READPUBEK cpu_to_be32(124)
845 struct tpm_input_header tpm_readpubek_header
= {
846 .tag
= TPM_TAG_RQU_COMMAND
,
847 .length
= cpu_to_be32(30),
848 .ordinal
= TPM_ORD_READPUBEK
851 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
855 struct tpm_cmd_t tpm_cmd
;
860 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
862 tpm_cmd
.header
.in
= tpm_readpubek_header
;
863 err
= transmit_cmd(chip
, &tpm_cmd
, READ_PUBEK_RESULT_SIZE
,
864 "attempting to read the PUBEK");
869 ignore header 10 bytes
870 algorithm 32 bits (1 == RSA )
873 parameters (RSA 12->bytes: keybit, #primes, expbit)
876 ignore checksum 20 bytes
878 data
= tpm_cmd
.params
.readpubek_out_buffer
;
881 "Algorithm: %02X %02X %02X %02X\n"
882 "Encscheme: %02X %02X\n"
883 "Sigscheme: %02X %02X\n"
884 "Parameters: %02X %02X %02X %02X "
885 "%02X %02X %02X %02X "
886 "%02X %02X %02X %02X\n"
887 "Modulus length: %d\n"
889 data
[0], data
[1], data
[2], data
[3],
892 data
[12], data
[13], data
[14], data
[15],
893 data
[16], data
[17], data
[18], data
[19],
894 data
[20], data
[21], data
[22], data
[23],
895 be32_to_cpu(*((__be32
*) (data
+ 24))));
897 for (i
= 0; i
< 256; i
++) {
898 str
+= sprintf(str
, "%02X ", data
[i
+ 28]);
899 if ((i
+ 1) % 16 == 0)
900 str
+= sprintf(str
, "\n");
906 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
909 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
916 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
917 "attempting to determine the manufacturer");
920 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
921 be32_to_cpu(cap
.manufacturer_id
));
923 rc
= tpm_getcap(dev
, CAP_VERSION_1_1
, &cap
,
924 "attempting to determine the 1.1 version");
928 "TCG version: %d.%d\nFirmware version: %d.%d\n",
929 cap
.tpm_version
.Major
, cap
.tpm_version
.Minor
,
930 cap
.tpm_version
.revMajor
, cap
.tpm_version
.revMinor
);
933 EXPORT_SYMBOL_GPL(tpm_show_caps
);
935 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
936 struct device_attribute
* attr
, char *buf
)
942 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
943 "attempting to determine the manufacturer");
946 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
947 be32_to_cpu(cap
.manufacturer_id
));
948 rc
= tpm_getcap(dev
, CAP_VERSION_1_2
, &cap
,
949 "attempting to determine the 1.2 version");
953 "TCG version: %d.%d\nFirmware version: %d.%d\n",
954 cap
.tpm_version_1_2
.Major
, cap
.tpm_version_1_2
.Minor
,
955 cap
.tpm_version_1_2
.revMajor
,
956 cap
.tpm_version_1_2
.revMinor
);
959 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
961 ssize_t
tpm_show_durations(struct device
*dev
, struct device_attribute
*attr
,
964 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
966 return sprintf(buf
, "%d %d %d [%s]\n",
967 jiffies_to_usecs(chip
->vendor
.duration
[TPM_SHORT
]),
968 jiffies_to_usecs(chip
->vendor
.duration
[TPM_MEDIUM
]),
969 jiffies_to_usecs(chip
->vendor
.duration
[TPM_LONG
]),
970 chip
->vendor
.duration_adjusted
971 ? "adjusted" : "original");
973 EXPORT_SYMBOL_GPL(tpm_show_durations
);
975 ssize_t
tpm_show_timeouts(struct device
*dev
, struct device_attribute
*attr
,
978 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
980 return sprintf(buf
, "%d %d %d %d [%s]\n",
981 jiffies_to_usecs(chip
->vendor
.timeout_a
),
982 jiffies_to_usecs(chip
->vendor
.timeout_b
),
983 jiffies_to_usecs(chip
->vendor
.timeout_c
),
984 jiffies_to_usecs(chip
->vendor
.timeout_d
),
985 chip
->vendor
.timeout_adjusted
986 ? "adjusted" : "original");
988 EXPORT_SYMBOL_GPL(tpm_show_timeouts
);
990 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
991 const char *buf
, size_t count
)
993 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
997 chip
->vendor
.cancel(chip
);
1000 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
1003 * Device file system interface to the TPM
1005 * It's assured that the chip will be opened just once,
1006 * by the check of is_open variable, which is protected
1009 int tpm_open(struct inode
*inode
, struct file
*file
)
1011 int minor
= iminor(inode
);
1012 struct tpm_chip
*chip
= NULL
, *pos
;
1015 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
1016 if (pos
->vendor
.miscdev
.minor
== minor
) {
1018 get_device(chip
->dev
);
1027 if (test_and_set_bit(0, &chip
->is_open
)) {
1028 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
1029 put_device(chip
->dev
);
1033 chip
->data_buffer
= kzalloc(TPM_BUFSIZE
, GFP_KERNEL
);
1034 if (chip
->data_buffer
== NULL
) {
1035 clear_bit(0, &chip
->is_open
);
1036 put_device(chip
->dev
);
1040 atomic_set(&chip
->data_pending
, 0);
1042 file
->private_data
= chip
;
1045 EXPORT_SYMBOL_GPL(tpm_open
);
1048 * Called on file close
1050 int tpm_release(struct inode
*inode
, struct file
*file
)
1052 struct tpm_chip
*chip
= file
->private_data
;
1054 del_singleshot_timer_sync(&chip
->user_read_timer
);
1055 flush_work_sync(&chip
->work
);
1056 file
->private_data
= NULL
;
1057 atomic_set(&chip
->data_pending
, 0);
1058 kfree(chip
->data_buffer
);
1059 clear_bit(0, &chip
->is_open
);
1060 put_device(chip
->dev
);
1063 EXPORT_SYMBOL_GPL(tpm_release
);
1065 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
1066 size_t size
, loff_t
*off
)
1068 struct tpm_chip
*chip
= file
->private_data
;
1069 size_t in_size
= size
, out_size
;
1071 /* cannot perform a write until the read has cleared
1072 either via tpm_read or a user_read_timer timeout */
1073 while (atomic_read(&chip
->data_pending
) != 0)
1074 msleep(TPM_TIMEOUT
);
1076 mutex_lock(&chip
->buffer_mutex
);
1078 if (in_size
> TPM_BUFSIZE
)
1079 in_size
= TPM_BUFSIZE
;
1082 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
1083 mutex_unlock(&chip
->buffer_mutex
);
1087 /* atomic tpm command send and result receive */
1088 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
1090 atomic_set(&chip
->data_pending
, out_size
);
1091 mutex_unlock(&chip
->buffer_mutex
);
1093 /* Set a timeout by which the reader must come claim the result */
1094 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
1098 EXPORT_SYMBOL_GPL(tpm_write
);
1100 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
1101 size_t size
, loff_t
*off
)
1103 struct tpm_chip
*chip
= file
->private_data
;
1106 del_singleshot_timer_sync(&chip
->user_read_timer
);
1107 flush_work_sync(&chip
->work
);
1108 ret_size
= atomic_read(&chip
->data_pending
);
1109 atomic_set(&chip
->data_pending
, 0);
1110 if (ret_size
> 0) { /* relay data */
1111 if (size
< ret_size
)
1114 mutex_lock(&chip
->buffer_mutex
);
1115 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1117 mutex_unlock(&chip
->buffer_mutex
);
1122 EXPORT_SYMBOL_GPL(tpm_read
);
1124 void tpm_remove_hardware(struct device
*dev
)
1126 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1129 dev_err(dev
, "No device data found\n");
1133 spin_lock(&driver_lock
);
1134 list_del_rcu(&chip
->list
);
1135 spin_unlock(&driver_lock
);
1138 misc_deregister(&chip
->vendor
.miscdev
);
1139 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1140 tpm_bios_log_teardown(chip
->bios_dir
);
1142 /* write it this way to be explicit (chip->dev == dev) */
1143 put_device(chip
->dev
);
1145 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1147 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1148 #define SAVESTATE_RESULT_SIZE 10
1150 static struct tpm_input_header savestate_header
= {
1151 .tag
= TPM_TAG_RQU_COMMAND
,
1152 .length
= cpu_to_be32(10),
1153 .ordinal
= TPM_ORD_SAVESTATE
1157 * We are about to suspend. Save the TPM state
1158 * so that it can be restored.
1160 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1162 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1163 struct tpm_cmd_t cmd
;
1166 u8 dummy_hash
[TPM_DIGEST_SIZE
] = { 0 };
1171 /* for buggy tpm, flush pcrs with extend to selected dummy */
1172 if (tpm_suspend_pcr
) {
1173 cmd
.header
.in
= pcrextend_header
;
1174 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(tpm_suspend_pcr
);
1175 memcpy(cmd
.params
.pcrextend_in
.hash
, dummy_hash
,
1177 rc
= transmit_cmd(chip
, &cmd
, EXTEND_PCR_RESULT_SIZE
,
1178 "extending dummy pcr before suspend");
1181 /* now do the actual savestate */
1182 cmd
.header
.in
= savestate_header
;
1183 rc
= transmit_cmd(chip
, &cmd
, SAVESTATE_RESULT_SIZE
,
1184 "sending savestate before suspend");
1187 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1190 * Resume from a power safe. The BIOS already restored
1193 int tpm_pm_resume(struct device
*dev
)
1195 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1202 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1204 /* In case vendor provided release function, call it too.*/
1206 void tpm_dev_vendor_release(struct tpm_chip
*chip
)
1208 if (chip
->vendor
.release
)
1209 chip
->vendor
.release(chip
->dev
);
1211 clear_bit(chip
->dev_num
, dev_mask
);
1212 kfree(chip
->vendor
.miscdev
.name
);
1214 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release
);
1218 * Once all references to platform device are down to 0,
1219 * release all allocated structures.
1221 void tpm_dev_release(struct device
*dev
)
1223 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1225 tpm_dev_vendor_release(chip
);
1230 EXPORT_SYMBOL_GPL(tpm_dev_release
);
1233 * Called from tpm_<specific>.c probe function only for devices
1234 * the driver has determined it should claim. Prior to calling
1235 * this function the specific probe function has called pci_enable_device
1236 * upon errant exit from this function specific probe function should call
1237 * pci_disable_device
1239 struct tpm_chip
*tpm_register_hardware(struct device
*dev
,
1240 const struct tpm_vendor_specific
*entry
)
1242 #define DEVNAME_SIZE 7
1245 struct tpm_chip
*chip
;
1247 /* Driver specific per-device data */
1248 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1249 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1251 if (chip
== NULL
|| devname
== NULL
)
1254 mutex_init(&chip
->buffer_mutex
);
1255 mutex_init(&chip
->tpm_mutex
);
1256 INIT_LIST_HEAD(&chip
->list
);
1258 INIT_WORK(&chip
->work
, timeout_work
);
1260 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1261 (unsigned long)chip
);
1263 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1265 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1267 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1268 dev_err(dev
, "No available tpm device numbers\n");
1270 } else if (chip
->dev_num
== 0)
1271 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1273 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1275 set_bit(chip
->dev_num
, dev_mask
);
1277 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1278 chip
->vendor
.miscdev
.name
= devname
;
1280 chip
->vendor
.miscdev
.parent
= dev
;
1281 chip
->dev
= get_device(dev
);
1282 chip
->release
= dev
->release
;
1283 dev
->release
= tpm_dev_release
;
1284 dev_set_drvdata(dev
, chip
);
1286 if (misc_register(&chip
->vendor
.miscdev
)) {
1288 "unable to misc_register %s, minor %d\n",
1289 chip
->vendor
.miscdev
.name
,
1290 chip
->vendor
.miscdev
.minor
);
1291 put_device(chip
->dev
);
1295 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1296 misc_deregister(&chip
->vendor
.miscdev
);
1297 put_device(chip
->dev
);
1302 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1304 /* Make chip available */
1305 spin_lock(&driver_lock
);
1306 list_add_rcu(&chip
->list
, &tpm_chip_list
);
1307 spin_unlock(&driver_lock
);
1316 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1318 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1319 MODULE_DESCRIPTION("TPM Driver");
1320 MODULE_VERSION("2.0");
1321 MODULE_LICENSE("GPL");