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/mutex.h>
28 #include <linux/spinlock.h>
33 TPM_MINOR
= 224, /* officially assigned */
35 TPM_NUM_DEVICES
= 256,
45 #define TPM_MAX_ORDINAL 243
46 #define TPM_MAX_PROTECTED_ORDINAL 12
47 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
49 static LIST_HEAD(tpm_chip_list
);
50 static DEFINE_SPINLOCK(driver_lock
);
51 static DECLARE_BITMAP(dev_mask
, TPM_NUM_DEVICES
);
54 * Array with one entry per ordinal defining the maximum amount
55 * of time the chip could take to return the result. The ordinal
56 * designation of short, medium or long is defined in a table in
57 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
58 * values of the SHORT, MEDIUM, and LONG durations are retrieved
59 * from the chip during initialization with a call to tpm_get_timeouts.
61 static const u8 tpm_protected_ordinal_duration
[TPM_MAX_PROTECTED_ORDINAL
] = {
62 TPM_UNDEFINED
, /* 0 */
67 TPM_UNDEFINED
, /* 5 */
76 static const u8 tpm_ordinal_duration
[TPM_MAX_ORDINAL
] = {
77 TPM_UNDEFINED
, /* 0 */
82 TPM_UNDEFINED
, /* 5 */
132 TPM_UNDEFINED
, /* 55 */
152 TPM_UNDEFINED
, /* 75 */
162 TPM_UNDEFINED
, /* 85 */
172 TPM_UNDEFINED
, /* 95 */
177 TPM_MEDIUM
, /* 100 */
182 TPM_UNDEFINED
, /* 105 */
212 TPM_UNDEFINED
, /* 135 */
222 TPM_UNDEFINED
, /* 145 */
232 TPM_UNDEFINED
, /* 155 */
242 TPM_UNDEFINED
, /* 165 */
252 TPM_UNDEFINED
, /* 175 */
257 TPM_MEDIUM
, /* 180 */
262 TPM_MEDIUM
, /* 185 */
267 TPM_UNDEFINED
, /* 190 */
272 TPM_UNDEFINED
, /* 195 */
287 TPM_MEDIUM
, /* 210 */
292 TPM_UNDEFINED
, /* 215 */
302 TPM_UNDEFINED
, /* 225 */
312 TPM_UNDEFINED
, /* 235 */
322 static void user_reader_timeout(unsigned long ptr
)
324 struct tpm_chip
*chip
= (struct tpm_chip
*) ptr
;
326 schedule_work(&chip
->work
);
329 static void timeout_work(struct work_struct
*work
)
331 struct tpm_chip
*chip
= container_of(work
, struct tpm_chip
, work
);
333 mutex_lock(&chip
->buffer_mutex
);
334 atomic_set(&chip
->data_pending
, 0);
335 memset(chip
->data_buffer
, 0, TPM_BUFSIZE
);
336 mutex_unlock(&chip
->buffer_mutex
);
340 * Returns max number of jiffies to wait
342 unsigned long tpm_calc_ordinal_duration(struct tpm_chip
*chip
,
345 int duration_idx
= TPM_UNDEFINED
;
348 if (ordinal
< TPM_MAX_ORDINAL
)
349 duration_idx
= tpm_ordinal_duration
[ordinal
];
350 else if ((ordinal
& TPM_PROTECTED_ORDINAL_MASK
) <
351 TPM_MAX_PROTECTED_ORDINAL
)
353 tpm_protected_ordinal_duration
[ordinal
&
354 TPM_PROTECTED_ORDINAL_MASK
];
356 if (duration_idx
!= TPM_UNDEFINED
) {
357 duration
= chip
->vendor
.duration
[duration_idx
];
358 /* if duration is 0, it's because chip->vendor.duration wasn't */
359 /* filled yet, so we set the lowest timeout just to give enough */
360 /* time for tpm_get_timeouts() to succeed */
361 return (duration
<= 0 ? HZ
: duration
);
365 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration
);
368 * Internal kernel interface to transmit TPM commands
370 static ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
377 if (bufsiz
> TPM_BUFSIZE
)
378 bufsiz
= TPM_BUFSIZE
;
380 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
381 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
384 if (count
> bufsiz
) {
386 "invalid count value %x %zx \n", count
, bufsiz
);
390 mutex_lock(&chip
->tpm_mutex
);
392 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
394 "tpm_transmit: tpm_send: error %zd\n", rc
);
398 if (chip
->vendor
.irq
)
401 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
403 u8 status
= chip
->vendor
.status(chip
);
404 if ((status
& chip
->vendor
.req_complete_mask
) ==
405 chip
->vendor
.req_complete_val
)
408 if ((status
== chip
->vendor
.req_canceled
)) {
409 dev_err(chip
->dev
, "Operation Canceled\n");
414 msleep(TPM_TIMEOUT
); /* CHECK */
416 } while (time_before(jiffies
, stop
));
418 chip
->vendor
.cancel(chip
);
419 dev_err(chip
->dev
, "Operation Timed out\n");
424 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
427 "tpm_transmit: tpm_recv: error %zd\n", rc
);
429 mutex_unlock(&chip
->tpm_mutex
);
433 #define TPM_DIGEST_SIZE 20
434 #define TPM_ERROR_SIZE 10
435 #define TPM_RET_CODE_IDX 6
437 enum tpm_capabilities
{
438 TPM_CAP_FLAG
= cpu_to_be32(4),
439 TPM_CAP_PROP
= cpu_to_be32(5),
440 CAP_VERSION_1_1
= cpu_to_be32(0x06),
441 CAP_VERSION_1_2
= cpu_to_be32(0x1A)
444 enum tpm_sub_capabilities
{
445 TPM_CAP_PROP_PCR
= cpu_to_be32(0x101),
446 TPM_CAP_PROP_MANUFACTURER
= cpu_to_be32(0x103),
447 TPM_CAP_FLAG_PERM
= cpu_to_be32(0x108),
448 TPM_CAP_FLAG_VOL
= cpu_to_be32(0x109),
449 TPM_CAP_PROP_OWNER
= cpu_to_be32(0x111),
450 TPM_CAP_PROP_TIS_TIMEOUT
= cpu_to_be32(0x115),
451 TPM_CAP_PROP_TIS_DURATION
= cpu_to_be32(0x120),
455 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, struct tpm_cmd_t
*cmd
,
456 int len
, const char *desc
)
460 len
= tpm_transmit(chip
,(u8
*) cmd
, len
);
463 if (len
== TPM_ERROR_SIZE
) {
464 err
= be32_to_cpu(cmd
->header
.out
.return_code
);
465 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
471 #define TPM_INTERNAL_RESULT_SIZE 200
472 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
473 #define TPM_ORD_GET_CAP cpu_to_be32(101)
475 static const struct tpm_input_header tpm_getcap_header
= {
476 .tag
= TPM_TAG_RQU_COMMAND
,
477 .length
= cpu_to_be32(22),
478 .ordinal
= TPM_ORD_GET_CAP
481 ssize_t
tpm_getcap(struct device
*dev
, __be32 subcap_id
, cap_t
*cap
,
484 struct tpm_cmd_t tpm_cmd
;
486 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
488 tpm_cmd
.header
.in
= tpm_getcap_header
;
489 if (subcap_id
== CAP_VERSION_1_1
|| subcap_id
== CAP_VERSION_1_2
) {
490 tpm_cmd
.params
.getcap_in
.cap
= subcap_id
;
491 /*subcap field not necessary */
492 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(0);
493 tpm_cmd
.header
.in
.length
-= cpu_to_be32(sizeof(__be32
));
495 if (subcap_id
== TPM_CAP_FLAG_PERM
||
496 subcap_id
== TPM_CAP_FLAG_VOL
)
497 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_FLAG
;
499 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
500 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
501 tpm_cmd
.params
.getcap_in
.subcap
= subcap_id
;
503 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
, desc
);
505 *cap
= tpm_cmd
.params
.getcap_out
.cap
;
509 void tpm_gen_interrupt(struct tpm_chip
*chip
)
511 struct tpm_cmd_t tpm_cmd
;
514 tpm_cmd
.header
.in
= tpm_getcap_header
;
515 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
516 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
517 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
519 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
520 "attempting to determine the timeouts");
522 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
524 void tpm_get_timeouts(struct tpm_chip
*chip
)
526 struct tpm_cmd_t tpm_cmd
;
527 struct timeout_t
*timeout_cap
;
528 struct duration_t
*duration_cap
;
532 tpm_cmd
.header
.in
= tpm_getcap_header
;
533 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
534 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
535 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
537 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
538 "attempting to determine the timeouts");
542 if (be32_to_cpu(tpm_cmd
.header
.out
.length
)
546 timeout_cap
= &tpm_cmd
.params
.getcap_out
.cap
.timeout
;
547 /* Don't overwrite default if value is 0 */
548 timeout
= be32_to_cpu(timeout_cap
->a
);
550 chip
->vendor
.timeout_a
= usecs_to_jiffies(timeout
);
551 timeout
= be32_to_cpu(timeout_cap
->b
);
553 chip
->vendor
.timeout_b
= usecs_to_jiffies(timeout
);
554 timeout
= be32_to_cpu(timeout_cap
->c
);
556 chip
->vendor
.timeout_c
= usecs_to_jiffies(timeout
);
557 timeout
= be32_to_cpu(timeout_cap
->d
);
559 chip
->vendor
.timeout_d
= usecs_to_jiffies(timeout
);
562 tpm_cmd
.header
.in
= tpm_getcap_header
;
563 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
564 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
565 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_DURATION
;
567 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
568 "attempting to determine the durations");
572 if (be32_to_cpu(tpm_cmd
.header
.out
.return_code
) != 0 ||
573 be32_to_cpu(tpm_cmd
.header
.out
.length
)
574 != sizeof(tpm_cmd
.header
.out
) + sizeof(u32
) + 3 * sizeof(u32
))
577 duration_cap
= &tpm_cmd
.params
.getcap_out
.cap
.duration
;
578 chip
->vendor
.duration
[TPM_SHORT
] =
579 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_short
));
580 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
581 * value wrong and apparently reports msecs rather than usecs. So we
582 * fix up the resulting too-small TPM_SHORT value to make things work.
584 if (chip
->vendor
.duration
[TPM_SHORT
] < (HZ
/100))
585 chip
->vendor
.duration
[TPM_SHORT
] = HZ
;
587 chip
->vendor
.duration
[TPM_MEDIUM
] =
588 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_medium
));
589 chip
->vendor
.duration
[TPM_LONG
] =
590 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_long
));
592 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
594 void tpm_continue_selftest(struct tpm_chip
*chip
)
597 0, 193, /* TPM_TAG_RQU_COMMAND */
598 0, 0, 0, 10, /* length */
599 0, 0, 0, 83, /* TPM_ORD_GetCapability */
602 tpm_transmit(chip
, data
, sizeof(data
));
604 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
606 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
612 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
613 "attempting to determine the permanent enabled state");
617 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.disable
);
620 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
622 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
628 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
629 "attempting to determine the permanent active state");
633 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.deactivated
);
636 EXPORT_SYMBOL_GPL(tpm_show_active
);
638 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
644 rc
= tpm_getcap(dev
, TPM_CAP_PROP_OWNER
, &cap
,
645 "attempting to determine the owner state");
649 rc
= sprintf(buf
, "%d\n", cap
.owned
);
652 EXPORT_SYMBOL_GPL(tpm_show_owned
);
654 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
655 struct device_attribute
* attr
, char *buf
)
660 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_VOL
, &cap
,
661 "attempting to determine the temporary state");
665 rc
= sprintf(buf
, "%d\n", cap
.stclear_flags
.deactivated
);
668 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
671 * tpm_chip_find_get - return tpm_chip for given chip number
673 static struct tpm_chip
*tpm_chip_find_get(int chip_num
)
675 struct tpm_chip
*pos
, *chip
= NULL
;
678 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
679 if (chip_num
!= TPM_ANY_NUM
&& chip_num
!= pos
->dev_num
)
682 if (try_module_get(pos
->dev
->driver
->owner
)) {
691 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
692 #define READ_PCR_RESULT_SIZE 30
693 static struct tpm_input_header pcrread_header
= {
694 .tag
= TPM_TAG_RQU_COMMAND
,
695 .length
= cpu_to_be32(14),
696 .ordinal
= TPM_ORDINAL_PCRREAD
699 int __tpm_pcr_read(struct tpm_chip
*chip
, int pcr_idx
, u8
*res_buf
)
702 struct tpm_cmd_t cmd
;
704 cmd
.header
.in
= pcrread_header
;
705 cmd
.params
.pcrread_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
706 rc
= transmit_cmd(chip
, &cmd
, READ_PCR_RESULT_SIZE
,
707 "attempting to read a pcr value");
710 memcpy(res_buf
, cmd
.params
.pcrread_out
.pcr_result
,
716 * tpm_pcr_read - read a pcr value
717 * @chip_num: tpm idx # or ANY
718 * @pcr_idx: pcr idx to retrieve
719 * @res_buf: TPM_PCR value
720 * size of res_buf is 20 bytes (or NULL if you don't care)
722 * The TPM driver should be built-in, but for whatever reason it
723 * isn't, protect against the chip disappearing, by incrementing
724 * the module usage count.
726 int tpm_pcr_read(u32 chip_num
, int pcr_idx
, u8
*res_buf
)
728 struct tpm_chip
*chip
;
731 chip
= tpm_chip_find_get(chip_num
);
734 rc
= __tpm_pcr_read(chip
, pcr_idx
, res_buf
);
735 module_put(chip
->dev
->driver
->owner
);
738 EXPORT_SYMBOL_GPL(tpm_pcr_read
);
741 * tpm_pcr_extend - extend pcr value with hash
742 * @chip_num: tpm idx # or AN&
743 * @pcr_idx: pcr idx to extend
744 * @hash: hash value used to extend pcr value
746 * The TPM driver should be built-in, but for whatever reason it
747 * isn't, protect against the chip disappearing, by incrementing
748 * the module usage count.
750 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
751 #define EXTEND_PCR_RESULT_SIZE 34
752 static struct tpm_input_header pcrextend_header
= {
753 .tag
= TPM_TAG_RQU_COMMAND
,
754 .length
= cpu_to_be32(34),
755 .ordinal
= TPM_ORD_PCR_EXTEND
758 int tpm_pcr_extend(u32 chip_num
, int pcr_idx
, const u8
*hash
)
760 struct tpm_cmd_t cmd
;
762 struct tpm_chip
*chip
;
764 chip
= tpm_chip_find_get(chip_num
);
768 cmd
.header
.in
= pcrextend_header
;
769 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
770 memcpy(cmd
.params
.pcrextend_in
.hash
, hash
, TPM_DIGEST_SIZE
);
771 rc
= transmit_cmd(chip
, &cmd
, EXTEND_PCR_RESULT_SIZE
,
772 "attempting extend a PCR value");
774 module_put(chip
->dev
->driver
->owner
);
777 EXPORT_SYMBOL_GPL(tpm_pcr_extend
);
779 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
783 u8 digest
[TPM_DIGEST_SIZE
];
787 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
789 rc
= tpm_getcap(dev
, TPM_CAP_PROP_PCR
, &cap
,
790 "attempting to determine the number of PCRS");
794 num_pcrs
= be32_to_cpu(cap
.num_pcrs
);
795 for (i
= 0; i
< num_pcrs
; i
++) {
796 rc
= __tpm_pcr_read(chip
, i
, digest
);
799 str
+= sprintf(str
, "PCR-%02d: ", i
);
800 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
801 str
+= sprintf(str
, "%02X ", digest
[j
]);
802 str
+= sprintf(str
, "\n");
806 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
808 #define READ_PUBEK_RESULT_SIZE 314
809 #define TPM_ORD_READPUBEK cpu_to_be32(124)
810 struct tpm_input_header tpm_readpubek_header
= {
811 .tag
= TPM_TAG_RQU_COMMAND
,
812 .length
= cpu_to_be32(30),
813 .ordinal
= TPM_ORD_READPUBEK
816 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
820 struct tpm_cmd_t tpm_cmd
;
825 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
827 tpm_cmd
.header
.in
= tpm_readpubek_header
;
828 err
= transmit_cmd(chip
, &tpm_cmd
, READ_PUBEK_RESULT_SIZE
,
829 "attempting to read the PUBEK");
834 ignore header 10 bytes
835 algorithm 32 bits (1 == RSA )
838 parameters (RSA 12->bytes: keybit, #primes, expbit)
841 ignore checksum 20 bytes
843 data
= tpm_cmd
.params
.readpubek_out_buffer
;
846 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
847 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
848 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
849 "Modulus length: %d\nModulus: \n",
850 data
[10], data
[11], data
[12], data
[13], data
[14],
851 data
[15], data
[16], data
[17], data
[22], data
[23],
852 data
[24], data
[25], data
[26], data
[27], data
[28],
853 data
[29], data
[30], data
[31], data
[32], data
[33],
854 be32_to_cpu(*((__be32
*) (data
+ 34))));
856 for (i
= 0; i
< 256; i
++) {
857 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
858 if ((i
+ 1) % 16 == 0)
859 str
+= sprintf(str
, "\n");
865 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
868 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
875 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
876 "attempting to determine the manufacturer");
879 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
880 be32_to_cpu(cap
.manufacturer_id
));
882 rc
= tpm_getcap(dev
, CAP_VERSION_1_1
, &cap
,
883 "attempting to determine the 1.1 version");
887 "TCG version: %d.%d\nFirmware version: %d.%d\n",
888 cap
.tpm_version
.Major
, cap
.tpm_version
.Minor
,
889 cap
.tpm_version
.revMajor
, cap
.tpm_version
.revMinor
);
892 EXPORT_SYMBOL_GPL(tpm_show_caps
);
894 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
895 struct device_attribute
* attr
, char *buf
)
901 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
902 "attempting to determine the manufacturer");
905 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
906 be32_to_cpu(cap
.manufacturer_id
));
907 rc
= tpm_getcap(dev
, CAP_VERSION_1_2
, &cap
,
908 "attempting to determine the 1.2 version");
912 "TCG version: %d.%d\nFirmware version: %d.%d\n",
913 cap
.tpm_version_1_2
.Major
, cap
.tpm_version_1_2
.Minor
,
914 cap
.tpm_version_1_2
.revMajor
,
915 cap
.tpm_version_1_2
.revMinor
);
918 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
920 ssize_t
tpm_show_timeouts(struct device
*dev
, struct device_attribute
*attr
,
923 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
925 return sprintf(buf
, "%d %d %d\n",
926 jiffies_to_usecs(chip
->vendor
.duration
[TPM_SHORT
]),
927 jiffies_to_usecs(chip
->vendor
.duration
[TPM_MEDIUM
]),
928 jiffies_to_usecs(chip
->vendor
.duration
[TPM_LONG
]));
930 EXPORT_SYMBOL_GPL(tpm_show_timeouts
);
932 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
933 const char *buf
, size_t count
)
935 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
939 chip
->vendor
.cancel(chip
);
942 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
945 * Device file system interface to the TPM
947 * It's assured that the chip will be opened just once,
948 * by the check of is_open variable, which is protected
951 int tpm_open(struct inode
*inode
, struct file
*file
)
953 int minor
= iminor(inode
);
954 struct tpm_chip
*chip
= NULL
, *pos
;
957 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
958 if (pos
->vendor
.miscdev
.minor
== minor
) {
960 get_device(chip
->dev
);
969 if (test_and_set_bit(0, &chip
->is_open
)) {
970 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
971 put_device(chip
->dev
);
975 chip
->data_buffer
= kzalloc(TPM_BUFSIZE
, GFP_KERNEL
);
976 if (chip
->data_buffer
== NULL
) {
977 clear_bit(0, &chip
->is_open
);
978 put_device(chip
->dev
);
982 atomic_set(&chip
->data_pending
, 0);
984 file
->private_data
= chip
;
987 EXPORT_SYMBOL_GPL(tpm_open
);
990 * Called on file close
992 int tpm_release(struct inode
*inode
, struct file
*file
)
994 struct tpm_chip
*chip
= file
->private_data
;
996 del_singleshot_timer_sync(&chip
->user_read_timer
);
997 flush_scheduled_work();
998 file
->private_data
= NULL
;
999 atomic_set(&chip
->data_pending
, 0);
1000 kfree(chip
->data_buffer
);
1001 clear_bit(0, &chip
->is_open
);
1002 put_device(chip
->dev
);
1005 EXPORT_SYMBOL_GPL(tpm_release
);
1007 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
1008 size_t size
, loff_t
*off
)
1010 struct tpm_chip
*chip
= file
->private_data
;
1011 size_t in_size
= size
, out_size
;
1013 /* cannot perform a write until the read has cleared
1014 either via tpm_read or a user_read_timer timeout */
1015 while (atomic_read(&chip
->data_pending
) != 0)
1016 msleep(TPM_TIMEOUT
);
1018 mutex_lock(&chip
->buffer_mutex
);
1020 if (in_size
> TPM_BUFSIZE
)
1021 in_size
= TPM_BUFSIZE
;
1024 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
1025 mutex_unlock(&chip
->buffer_mutex
);
1029 /* atomic tpm command send and result receive */
1030 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
1032 atomic_set(&chip
->data_pending
, out_size
);
1033 mutex_unlock(&chip
->buffer_mutex
);
1035 /* Set a timeout by which the reader must come claim the result */
1036 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
1040 EXPORT_SYMBOL_GPL(tpm_write
);
1042 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
1043 size_t size
, loff_t
*off
)
1045 struct tpm_chip
*chip
= file
->private_data
;
1049 del_singleshot_timer_sync(&chip
->user_read_timer
);
1050 flush_scheduled_work();
1051 ret_size
= atomic_read(&chip
->data_pending
);
1052 atomic_set(&chip
->data_pending
, 0);
1053 if (ret_size
> 0) { /* relay data */
1054 if (size
< ret_size
)
1057 mutex_lock(&chip
->buffer_mutex
);
1058 rc
= copy_to_user(buf
, chip
->data_buffer
, ret_size
);
1059 memset(chip
->data_buffer
, 0, ret_size
);
1063 mutex_unlock(&chip
->buffer_mutex
);
1068 EXPORT_SYMBOL_GPL(tpm_read
);
1070 void tpm_remove_hardware(struct device
*dev
)
1072 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1075 dev_err(dev
, "No device data found\n");
1079 spin_lock(&driver_lock
);
1080 list_del_rcu(&chip
->list
);
1081 spin_unlock(&driver_lock
);
1084 misc_deregister(&chip
->vendor
.miscdev
);
1085 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1086 tpm_bios_log_teardown(chip
->bios_dir
);
1088 /* write it this way to be explicit (chip->dev == dev) */
1089 put_device(chip
->dev
);
1091 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1094 * We are about to suspend. Save the TPM state
1095 * so that it can be restored.
1097 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1099 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1101 0, 193, /* TPM_TAG_RQU_COMMAND */
1102 0, 0, 0, 10, /* blob length (in bytes) */
1103 0, 0, 0, 152 /* TPM_ORD_SaveState */
1109 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1112 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1115 * Resume from a power safe. The BIOS already restored
1118 int tpm_pm_resume(struct device
*dev
)
1120 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1127 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1129 /* In case vendor provided release function, call it too.*/
1131 void tpm_dev_vendor_release(struct tpm_chip
*chip
)
1133 if (chip
->vendor
.release
)
1134 chip
->vendor
.release(chip
->dev
);
1136 clear_bit(chip
->dev_num
, dev_mask
);
1137 kfree(chip
->vendor
.miscdev
.name
);
1139 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release
);
1143 * Once all references to platform device are down to 0,
1144 * release all allocated structures.
1146 void tpm_dev_release(struct device
*dev
)
1148 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1150 tpm_dev_vendor_release(chip
);
1155 EXPORT_SYMBOL_GPL(tpm_dev_release
);
1158 * Called from tpm_<specific>.c probe function only for devices
1159 * the driver has determined it should claim. Prior to calling
1160 * this function the specific probe function has called pci_enable_device
1161 * upon errant exit from this function specific probe function should call
1162 * pci_disable_device
1164 struct tpm_chip
*tpm_register_hardware(struct device
*dev
,
1165 const struct tpm_vendor_specific
*entry
)
1167 #define DEVNAME_SIZE 7
1170 struct tpm_chip
*chip
;
1172 /* Driver specific per-device data */
1173 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1174 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1176 if (chip
== NULL
|| devname
== NULL
)
1179 mutex_init(&chip
->buffer_mutex
);
1180 mutex_init(&chip
->tpm_mutex
);
1181 INIT_LIST_HEAD(&chip
->list
);
1183 INIT_WORK(&chip
->work
, timeout_work
);
1185 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1186 (unsigned long)chip
);
1188 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1190 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1192 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1193 dev_err(dev
, "No available tpm device numbers\n");
1195 } else if (chip
->dev_num
== 0)
1196 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1198 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1200 set_bit(chip
->dev_num
, dev_mask
);
1202 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1203 chip
->vendor
.miscdev
.name
= devname
;
1205 chip
->vendor
.miscdev
.parent
= dev
;
1206 chip
->dev
= get_device(dev
);
1207 chip
->release
= dev
->release
;
1208 dev
->release
= tpm_dev_release
;
1209 dev_set_drvdata(dev
, chip
);
1211 if (misc_register(&chip
->vendor
.miscdev
)) {
1213 "unable to misc_register %s, minor %d\n",
1214 chip
->vendor
.miscdev
.name
,
1215 chip
->vendor
.miscdev
.minor
);
1216 put_device(chip
->dev
);
1220 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1221 misc_deregister(&chip
->vendor
.miscdev
);
1222 put_device(chip
->dev
);
1227 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1229 /* Make chip available */
1230 spin_lock(&driver_lock
);
1231 list_add_rcu(&chip
->list
, &tpm_chip_list
);
1232 spin_unlock(&driver_lock
);
1241 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1243 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1244 MODULE_DESCRIPTION("TPM Driver");
1245 MODULE_VERSION("2.0");
1246 MODULE_LICENSE("GPL");