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
];
369 /* if duration is 0, it's because chip->vendor.duration wasn't */
370 /* filled yet, so we set the lowest timeout just to give enough */
371 /* time for tpm_get_timeouts() to succeed */
372 return (duration
<= 0 ? HZ
: duration
);
376 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration
);
379 * Internal kernel interface to transmit TPM commands
381 static ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
388 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
389 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
392 if (count
> bufsiz
) {
394 "invalid count value %x %zx \n", count
, bufsiz
);
398 mutex_lock(&chip
->tpm_mutex
);
400 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
402 "tpm_transmit: tpm_send: error %zd\n", rc
);
406 if (chip
->vendor
.irq
)
409 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
411 u8 status
= chip
->vendor
.status(chip
);
412 if ((status
& chip
->vendor
.req_complete_mask
) ==
413 chip
->vendor
.req_complete_val
)
416 if ((status
== chip
->vendor
.req_canceled
)) {
417 dev_err(chip
->dev
, "Operation Canceled\n");
422 msleep(TPM_TIMEOUT
); /* CHECK */
424 } while (time_before(jiffies
, stop
));
426 chip
->vendor
.cancel(chip
);
427 dev_err(chip
->dev
, "Operation Timed out\n");
432 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
435 "tpm_transmit: tpm_recv: error %zd\n", rc
);
437 mutex_unlock(&chip
->tpm_mutex
);
441 #define TPM_DIGEST_SIZE 20
442 #define TPM_ERROR_SIZE 10
443 #define TPM_RET_CODE_IDX 6
445 enum tpm_capabilities
{
446 TPM_CAP_FLAG
= cpu_to_be32(4),
447 TPM_CAP_PROP
= cpu_to_be32(5),
448 CAP_VERSION_1_1
= cpu_to_be32(0x06),
449 CAP_VERSION_1_2
= cpu_to_be32(0x1A)
452 enum tpm_sub_capabilities
{
453 TPM_CAP_PROP_PCR
= cpu_to_be32(0x101),
454 TPM_CAP_PROP_MANUFACTURER
= cpu_to_be32(0x103),
455 TPM_CAP_FLAG_PERM
= cpu_to_be32(0x108),
456 TPM_CAP_FLAG_VOL
= cpu_to_be32(0x109),
457 TPM_CAP_PROP_OWNER
= cpu_to_be32(0x111),
458 TPM_CAP_PROP_TIS_TIMEOUT
= cpu_to_be32(0x115),
459 TPM_CAP_PROP_TIS_DURATION
= cpu_to_be32(0x120),
463 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, struct tpm_cmd_t
*cmd
,
464 int len
, const char *desc
)
468 len
= tpm_transmit(chip
,(u8
*) cmd
, len
);
471 if (len
== TPM_ERROR_SIZE
) {
472 err
= be32_to_cpu(cmd
->header
.out
.return_code
);
473 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
479 #define TPM_INTERNAL_RESULT_SIZE 200
480 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
481 #define TPM_ORD_GET_CAP cpu_to_be32(101)
483 static const struct tpm_input_header tpm_getcap_header
= {
484 .tag
= TPM_TAG_RQU_COMMAND
,
485 .length
= cpu_to_be32(22),
486 .ordinal
= TPM_ORD_GET_CAP
489 ssize_t
tpm_getcap(struct device
*dev
, __be32 subcap_id
, cap_t
*cap
,
492 struct tpm_cmd_t tpm_cmd
;
494 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
496 tpm_cmd
.header
.in
= tpm_getcap_header
;
497 if (subcap_id
== CAP_VERSION_1_1
|| subcap_id
== CAP_VERSION_1_2
) {
498 tpm_cmd
.params
.getcap_in
.cap
= subcap_id
;
499 /*subcap field not necessary */
500 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(0);
501 tpm_cmd
.header
.in
.length
-= cpu_to_be32(sizeof(__be32
));
503 if (subcap_id
== TPM_CAP_FLAG_PERM
||
504 subcap_id
== TPM_CAP_FLAG_VOL
)
505 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_FLAG
;
507 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
508 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
509 tpm_cmd
.params
.getcap_in
.subcap
= subcap_id
;
511 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
, desc
);
513 *cap
= tpm_cmd
.params
.getcap_out
.cap
;
517 void tpm_gen_interrupt(struct tpm_chip
*chip
)
519 struct tpm_cmd_t tpm_cmd
;
522 tpm_cmd
.header
.in
= tpm_getcap_header
;
523 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
524 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
525 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
527 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
528 "attempting to determine the timeouts");
530 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
532 void tpm_get_timeouts(struct tpm_chip
*chip
)
534 struct tpm_cmd_t tpm_cmd
;
535 struct timeout_t
*timeout_cap
;
536 struct duration_t
*duration_cap
;
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
.length
)
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
);
558 chip
->vendor
.timeout_a
= usecs_to_jiffies(timeout
);
559 timeout
= be32_to_cpu(timeout_cap
->b
);
561 chip
->vendor
.timeout_b
= usecs_to_jiffies(timeout
);
562 timeout
= be32_to_cpu(timeout_cap
->c
);
564 chip
->vendor
.timeout_c
= usecs_to_jiffies(timeout
);
565 timeout
= be32_to_cpu(timeout_cap
->d
);
567 chip
->vendor
.timeout_d
= usecs_to_jiffies(timeout
);
570 tpm_cmd
.header
.in
= tpm_getcap_header
;
571 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
572 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
573 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_DURATION
;
575 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
576 "attempting to determine the durations");
580 if (be32_to_cpu(tpm_cmd
.header
.out
.return_code
)
583 duration_cap
= &tpm_cmd
.params
.getcap_out
.cap
.duration
;
584 chip
->vendor
.duration
[TPM_SHORT
] =
585 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_short
));
586 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
587 * value wrong and apparently reports msecs rather than usecs. So we
588 * fix up the resulting too-small TPM_SHORT value to make things work.
590 if (chip
->vendor
.duration
[TPM_SHORT
] < (HZ
/100))
591 chip
->vendor
.duration
[TPM_SHORT
] = HZ
;
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 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
600 void tpm_continue_selftest(struct tpm_chip
*chip
)
603 0, 193, /* TPM_TAG_RQU_COMMAND */
604 0, 0, 0, 10, /* length */
605 0, 0, 0, 83, /* TPM_ORD_GetCapability */
608 tpm_transmit(chip
, data
, sizeof(data
));
610 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
612 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
618 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
619 "attempting to determine the permanent enabled state");
623 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.disable
);
626 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
628 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
634 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
635 "attempting to determine the permanent active state");
639 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.deactivated
);
642 EXPORT_SYMBOL_GPL(tpm_show_active
);
644 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
650 rc
= tpm_getcap(dev
, TPM_CAP_PROP_OWNER
, &cap
,
651 "attempting to determine the owner state");
655 rc
= sprintf(buf
, "%d\n", cap
.owned
);
658 EXPORT_SYMBOL_GPL(tpm_show_owned
);
660 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
661 struct device_attribute
* attr
, char *buf
)
666 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_VOL
, &cap
,
667 "attempting to determine the temporary state");
671 rc
= sprintf(buf
, "%d\n", cap
.stclear_flags
.deactivated
);
674 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
677 * tpm_chip_find_get - return tpm_chip for given chip number
679 static struct tpm_chip
*tpm_chip_find_get(int chip_num
)
681 struct tpm_chip
*pos
, *chip
= NULL
;
684 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
685 if (chip_num
!= TPM_ANY_NUM
&& chip_num
!= pos
->dev_num
)
688 if (try_module_get(pos
->dev
->driver
->owner
)) {
697 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
698 #define READ_PCR_RESULT_SIZE 30
699 static struct tpm_input_header pcrread_header
= {
700 .tag
= TPM_TAG_RQU_COMMAND
,
701 .length
= cpu_to_be32(14),
702 .ordinal
= TPM_ORDINAL_PCRREAD
705 int __tpm_pcr_read(struct tpm_chip
*chip
, int pcr_idx
, u8
*res_buf
)
708 struct tpm_cmd_t cmd
;
710 cmd
.header
.in
= pcrread_header
;
711 cmd
.params
.pcrread_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
712 rc
= transmit_cmd(chip
, &cmd
, READ_PCR_RESULT_SIZE
,
713 "attempting to read a pcr value");
716 memcpy(res_buf
, cmd
.params
.pcrread_out
.pcr_result
,
722 * tpm_pcr_read - read a pcr value
723 * @chip_num: tpm idx # or ANY
724 * @pcr_idx: pcr idx to retrieve
725 * @res_buf: TPM_PCR value
726 * size of res_buf is 20 bytes (or NULL if you don't care)
728 * The TPM driver should be built-in, but for whatever reason it
729 * isn't, protect against the chip disappearing, by incrementing
730 * the module usage count.
732 int tpm_pcr_read(u32 chip_num
, int pcr_idx
, u8
*res_buf
)
734 struct tpm_chip
*chip
;
737 chip
= tpm_chip_find_get(chip_num
);
740 rc
= __tpm_pcr_read(chip
, pcr_idx
, res_buf
);
744 EXPORT_SYMBOL_GPL(tpm_pcr_read
);
747 * tpm_pcr_extend - extend pcr value with hash
748 * @chip_num: tpm idx # or AN&
749 * @pcr_idx: pcr idx to extend
750 * @hash: hash value used to extend pcr value
752 * The TPM driver should be built-in, but for whatever reason it
753 * isn't, protect against the chip disappearing, by incrementing
754 * the module usage count.
756 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
757 #define EXTEND_PCR_RESULT_SIZE 34
758 static struct tpm_input_header pcrextend_header
= {
759 .tag
= TPM_TAG_RQU_COMMAND
,
760 .length
= cpu_to_be32(34),
761 .ordinal
= TPM_ORD_PCR_EXTEND
764 int tpm_pcr_extend(u32 chip_num
, int pcr_idx
, const u8
*hash
)
766 struct tpm_cmd_t cmd
;
768 struct tpm_chip
*chip
;
770 chip
= tpm_chip_find_get(chip_num
);
774 cmd
.header
.in
= pcrextend_header
;
775 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
776 memcpy(cmd
.params
.pcrextend_in
.hash
, hash
, TPM_DIGEST_SIZE
);
777 rc
= transmit_cmd(chip
, &cmd
, EXTEND_PCR_RESULT_SIZE
,
778 "attempting extend a PCR value");
783 EXPORT_SYMBOL_GPL(tpm_pcr_extend
);
785 int tpm_send(u32 chip_num
, void *cmd
, size_t buflen
)
787 struct tpm_chip
*chip
;
790 chip
= tpm_chip_find_get(chip_num
);
794 rc
= transmit_cmd(chip
, cmd
, buflen
, "attempting tpm_cmd");
799 EXPORT_SYMBOL_GPL(tpm_send
);
801 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
805 u8 digest
[TPM_DIGEST_SIZE
];
809 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
811 rc
= tpm_getcap(dev
, TPM_CAP_PROP_PCR
, &cap
,
812 "attempting to determine the number of PCRS");
816 num_pcrs
= be32_to_cpu(cap
.num_pcrs
);
817 for (i
= 0; i
< num_pcrs
; i
++) {
818 rc
= __tpm_pcr_read(chip
, i
, digest
);
821 str
+= sprintf(str
, "PCR-%02d: ", i
);
822 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
823 str
+= sprintf(str
, "%02X ", digest
[j
]);
824 str
+= sprintf(str
, "\n");
828 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
830 #define READ_PUBEK_RESULT_SIZE 314
831 #define TPM_ORD_READPUBEK cpu_to_be32(124)
832 struct tpm_input_header tpm_readpubek_header
= {
833 .tag
= TPM_TAG_RQU_COMMAND
,
834 .length
= cpu_to_be32(30),
835 .ordinal
= TPM_ORD_READPUBEK
838 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
842 struct tpm_cmd_t tpm_cmd
;
847 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
849 tpm_cmd
.header
.in
= tpm_readpubek_header
;
850 err
= transmit_cmd(chip
, &tpm_cmd
, READ_PUBEK_RESULT_SIZE
,
851 "attempting to read the PUBEK");
856 ignore header 10 bytes
857 algorithm 32 bits (1 == RSA )
860 parameters (RSA 12->bytes: keybit, #primes, expbit)
863 ignore checksum 20 bytes
865 data
= tpm_cmd
.params
.readpubek_out_buffer
;
868 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
869 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
870 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
871 "Modulus length: %d\nModulus: \n",
872 data
[10], data
[11], data
[12], data
[13], data
[14],
873 data
[15], data
[16], data
[17], data
[22], data
[23],
874 data
[24], data
[25], data
[26], data
[27], data
[28],
875 data
[29], data
[30], data
[31], data
[32], data
[33],
876 be32_to_cpu(*((__be32
*) (data
+ 34))));
878 for (i
= 0; i
< 256; i
++) {
879 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
880 if ((i
+ 1) % 16 == 0)
881 str
+= sprintf(str
, "\n");
887 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
890 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
897 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
898 "attempting to determine the manufacturer");
901 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
902 be32_to_cpu(cap
.manufacturer_id
));
904 rc
= tpm_getcap(dev
, CAP_VERSION_1_1
, &cap
,
905 "attempting to determine the 1.1 version");
909 "TCG version: %d.%d\nFirmware version: %d.%d\n",
910 cap
.tpm_version
.Major
, cap
.tpm_version
.Minor
,
911 cap
.tpm_version
.revMajor
, cap
.tpm_version
.revMinor
);
914 EXPORT_SYMBOL_GPL(tpm_show_caps
);
916 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
917 struct device_attribute
* attr
, char *buf
)
923 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
924 "attempting to determine the manufacturer");
927 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
928 be32_to_cpu(cap
.manufacturer_id
));
929 rc
= tpm_getcap(dev
, CAP_VERSION_1_2
, &cap
,
930 "attempting to determine the 1.2 version");
934 "TCG version: %d.%d\nFirmware version: %d.%d\n",
935 cap
.tpm_version_1_2
.Major
, cap
.tpm_version_1_2
.Minor
,
936 cap
.tpm_version_1_2
.revMajor
,
937 cap
.tpm_version_1_2
.revMinor
);
940 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
942 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
943 const char *buf
, size_t count
)
945 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
949 chip
->vendor
.cancel(chip
);
952 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
955 * Device file system interface to the TPM
957 * It's assured that the chip will be opened just once,
958 * by the check of is_open variable, which is protected
961 int tpm_open(struct inode
*inode
, struct file
*file
)
963 int minor
= iminor(inode
);
964 struct tpm_chip
*chip
= NULL
, *pos
;
967 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
968 if (pos
->vendor
.miscdev
.minor
== minor
) {
970 get_device(chip
->dev
);
979 if (test_and_set_bit(0, &chip
->is_open
)) {
980 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
981 put_device(chip
->dev
);
985 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
986 if (chip
->data_buffer
== NULL
) {
987 clear_bit(0, &chip
->is_open
);
988 put_device(chip
->dev
);
992 atomic_set(&chip
->data_pending
, 0);
994 file
->private_data
= chip
;
997 EXPORT_SYMBOL_GPL(tpm_open
);
1000 * Called on file close
1002 int tpm_release(struct inode
*inode
, struct file
*file
)
1004 struct tpm_chip
*chip
= file
->private_data
;
1006 del_singleshot_timer_sync(&chip
->user_read_timer
);
1007 flush_work_sync(&chip
->work
);
1008 file
->private_data
= NULL
;
1009 atomic_set(&chip
->data_pending
, 0);
1010 kfree(chip
->data_buffer
);
1011 clear_bit(0, &chip
->is_open
);
1012 put_device(chip
->dev
);
1015 EXPORT_SYMBOL_GPL(tpm_release
);
1017 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
1018 size_t size
, loff_t
*off
)
1020 struct tpm_chip
*chip
= file
->private_data
;
1021 size_t in_size
= size
, out_size
;
1023 /* cannot perform a write until the read has cleared
1024 either via tpm_read or a user_read_timer timeout */
1025 while (atomic_read(&chip
->data_pending
) != 0)
1026 msleep(TPM_TIMEOUT
);
1028 mutex_lock(&chip
->buffer_mutex
);
1030 if (in_size
> TPM_BUFSIZE
)
1031 in_size
= TPM_BUFSIZE
;
1034 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
1035 mutex_unlock(&chip
->buffer_mutex
);
1039 /* atomic tpm command send and result receive */
1040 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
1042 atomic_set(&chip
->data_pending
, out_size
);
1043 mutex_unlock(&chip
->buffer_mutex
);
1045 /* Set a timeout by which the reader must come claim the result */
1046 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
1050 EXPORT_SYMBOL_GPL(tpm_write
);
1052 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
1053 size_t size
, loff_t
*off
)
1055 struct tpm_chip
*chip
= file
->private_data
;
1058 del_singleshot_timer_sync(&chip
->user_read_timer
);
1059 flush_work_sync(&chip
->work
);
1060 ret_size
= atomic_read(&chip
->data_pending
);
1061 atomic_set(&chip
->data_pending
, 0);
1062 if (ret_size
> 0) { /* relay data */
1063 if (size
< ret_size
)
1066 mutex_lock(&chip
->buffer_mutex
);
1067 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1069 mutex_unlock(&chip
->buffer_mutex
);
1074 EXPORT_SYMBOL_GPL(tpm_read
);
1076 void tpm_remove_hardware(struct device
*dev
)
1078 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1081 dev_err(dev
, "No device data found\n");
1085 spin_lock(&driver_lock
);
1086 list_del_rcu(&chip
->list
);
1087 spin_unlock(&driver_lock
);
1090 misc_deregister(&chip
->vendor
.miscdev
);
1091 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1092 tpm_bios_log_teardown(chip
->bios_dir
);
1094 /* write it this way to be explicit (chip->dev == dev) */
1095 put_device(chip
->dev
);
1097 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1099 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1100 #define SAVESTATE_RESULT_SIZE 10
1102 static struct tpm_input_header savestate_header
= {
1103 .tag
= TPM_TAG_RQU_COMMAND
,
1104 .length
= cpu_to_be32(10),
1105 .ordinal
= TPM_ORD_SAVESTATE
1109 * We are about to suspend. Save the TPM state
1110 * so that it can be restored.
1112 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1114 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1115 struct tpm_cmd_t cmd
;
1118 u8 dummy_hash
[TPM_DIGEST_SIZE
] = { 0 };
1123 /* for buggy tpm, flush pcrs with extend to selected dummy */
1124 if (tpm_suspend_pcr
) {
1125 cmd
.header
.in
= pcrextend_header
;
1126 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(tpm_suspend_pcr
);
1127 memcpy(cmd
.params
.pcrextend_in
.hash
, dummy_hash
,
1129 rc
= transmit_cmd(chip
, &cmd
, EXTEND_PCR_RESULT_SIZE
,
1130 "extending dummy pcr before suspend");
1133 /* now do the actual savestate */
1134 cmd
.header
.in
= savestate_header
;
1135 rc
= transmit_cmd(chip
, &cmd
, SAVESTATE_RESULT_SIZE
,
1136 "sending savestate before suspend");
1139 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1142 * Resume from a power safe. The BIOS already restored
1145 int tpm_pm_resume(struct device
*dev
)
1147 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1154 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1156 /* In case vendor provided release function, call it too.*/
1158 void tpm_dev_vendor_release(struct tpm_chip
*chip
)
1160 if (chip
->vendor
.release
)
1161 chip
->vendor
.release(chip
->dev
);
1163 clear_bit(chip
->dev_num
, dev_mask
);
1164 kfree(chip
->vendor
.miscdev
.name
);
1166 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release
);
1170 * Once all references to platform device are down to 0,
1171 * release all allocated structures.
1173 void tpm_dev_release(struct device
*dev
)
1175 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1177 tpm_dev_vendor_release(chip
);
1182 EXPORT_SYMBOL_GPL(tpm_dev_release
);
1185 * Called from tpm_<specific>.c probe function only for devices
1186 * the driver has determined it should claim. Prior to calling
1187 * this function the specific probe function has called pci_enable_device
1188 * upon errant exit from this function specific probe function should call
1189 * pci_disable_device
1191 struct tpm_chip
*tpm_register_hardware(struct device
*dev
,
1192 const struct tpm_vendor_specific
*entry
)
1194 #define DEVNAME_SIZE 7
1197 struct tpm_chip
*chip
;
1199 /* Driver specific per-device data */
1200 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1201 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1203 if (chip
== NULL
|| devname
== NULL
)
1206 mutex_init(&chip
->buffer_mutex
);
1207 mutex_init(&chip
->tpm_mutex
);
1208 INIT_LIST_HEAD(&chip
->list
);
1210 INIT_WORK(&chip
->work
, timeout_work
);
1212 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1213 (unsigned long)chip
);
1215 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1217 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1219 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1220 dev_err(dev
, "No available tpm device numbers\n");
1222 } else if (chip
->dev_num
== 0)
1223 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1225 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1227 set_bit(chip
->dev_num
, dev_mask
);
1229 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1230 chip
->vendor
.miscdev
.name
= devname
;
1232 chip
->vendor
.miscdev
.parent
= dev
;
1233 chip
->dev
= get_device(dev
);
1234 chip
->release
= dev
->release
;
1235 dev
->release
= tpm_dev_release
;
1236 dev_set_drvdata(dev
, chip
);
1238 if (misc_register(&chip
->vendor
.miscdev
)) {
1240 "unable to misc_register %s, minor %d\n",
1241 chip
->vendor
.miscdev
.name
,
1242 chip
->vendor
.miscdev
.minor
);
1243 put_device(chip
->dev
);
1247 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1248 misc_deregister(&chip
->vendor
.miscdev
);
1249 put_device(chip
->dev
);
1254 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1256 /* Make chip available */
1257 spin_lock(&driver_lock
);
1258 list_add_rcu(&chip
->list
, &tpm_chip_list
);
1259 spin_unlock(&driver_lock
);
1268 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1270 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1271 MODULE_DESCRIPTION("TPM Driver");
1272 MODULE_VERSION("2.0");
1273 MODULE_LICENSE("GPL");