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/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
32 TPM_MINOR
= 224, /* officially assigned */
34 TPM_NUM_DEVICES
= 256,
44 #define TPM_MAX_ORDINAL 243
45 #define TPM_MAX_PROTECTED_ORDINAL 12
46 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
48 static LIST_HEAD(tpm_chip_list
);
49 static DEFINE_SPINLOCK(driver_lock
);
50 static DECLARE_BITMAP(dev_mask
, TPM_NUM_DEVICES
);
53 * Array with one entry per ordinal defining the maximum amount
54 * of time the chip could take to return the result. The ordinal
55 * designation of short, medium or long is defined in a table in
56 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
57 * values of the SHORT, MEDIUM, and LONG durations are retrieved
58 * from the chip during initialization with a call to tpm_get_timeouts.
60 static const u8 tpm_protected_ordinal_duration
[TPM_MAX_PROTECTED_ORDINAL
] = {
61 TPM_UNDEFINED
, /* 0 */
66 TPM_UNDEFINED
, /* 5 */
75 static const u8 tpm_ordinal_duration
[TPM_MAX_ORDINAL
] = {
76 TPM_UNDEFINED
, /* 0 */
81 TPM_UNDEFINED
, /* 5 */
131 TPM_UNDEFINED
, /* 55 */
151 TPM_UNDEFINED
, /* 75 */
161 TPM_UNDEFINED
, /* 85 */
171 TPM_UNDEFINED
, /* 95 */
176 TPM_MEDIUM
, /* 100 */
181 TPM_UNDEFINED
, /* 105 */
211 TPM_UNDEFINED
, /* 135 */
221 TPM_UNDEFINED
, /* 145 */
231 TPM_UNDEFINED
, /* 155 */
241 TPM_UNDEFINED
, /* 165 */
251 TPM_UNDEFINED
, /* 175 */
256 TPM_MEDIUM
, /* 180 */
261 TPM_MEDIUM
, /* 185 */
266 TPM_UNDEFINED
, /* 190 */
271 TPM_UNDEFINED
, /* 195 */
286 TPM_MEDIUM
, /* 210 */
291 TPM_UNDEFINED
, /* 215 */
301 TPM_UNDEFINED
, /* 225 */
311 TPM_UNDEFINED
, /* 235 */
321 static void user_reader_timeout(unsigned long ptr
)
323 struct tpm_chip
*chip
= (struct tpm_chip
*) ptr
;
325 schedule_work(&chip
->work
);
328 static void timeout_work(struct work_struct
*work
)
330 struct tpm_chip
*chip
= container_of(work
, struct tpm_chip
, work
);
332 down(&chip
->buffer_mutex
);
333 atomic_set(&chip
->data_pending
, 0);
334 memset(chip
->data_buffer
, 0, TPM_BUFSIZE
);
335 up(&chip
->buffer_mutex
);
339 * Returns max number of jiffies to wait
341 unsigned long tpm_calc_ordinal_duration(struct tpm_chip
*chip
,
344 int duration_idx
= TPM_UNDEFINED
;
347 if (ordinal
< TPM_MAX_ORDINAL
)
348 duration_idx
= tpm_ordinal_duration
[ordinal
];
349 else if ((ordinal
& TPM_PROTECTED_ORDINAL_MASK
) <
350 TPM_MAX_PROTECTED_ORDINAL
)
352 tpm_protected_ordinal_duration
[ordinal
&
353 TPM_PROTECTED_ORDINAL_MASK
];
355 if (duration_idx
!= TPM_UNDEFINED
)
356 duration
= chip
->vendor
.duration
[duration_idx
];
362 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration
);
365 * Internal kernel interface to transmit TPM commands
367 static ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
374 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
375 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
378 if (count
> bufsiz
) {
380 "invalid count value %x %zx \n", count
, bufsiz
);
384 down(&chip
->tpm_mutex
);
386 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
388 "tpm_transmit: tpm_send: error %zd\n", rc
);
392 if (chip
->vendor
.irq
)
395 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
397 u8 status
= chip
->vendor
.status(chip
);
398 if ((status
& chip
->vendor
.req_complete_mask
) ==
399 chip
->vendor
.req_complete_val
)
402 if ((status
== chip
->vendor
.req_canceled
)) {
403 dev_err(chip
->dev
, "Operation Canceled\n");
408 msleep(TPM_TIMEOUT
); /* CHECK */
410 } while (time_before(jiffies
, stop
));
412 chip
->vendor
.cancel(chip
);
413 dev_err(chip
->dev
, "Operation Timed out\n");
418 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
421 "tpm_transmit: tpm_recv: error %zd\n", rc
);
423 up(&chip
->tpm_mutex
);
427 #define TPM_DIGEST_SIZE 20
428 #define TPM_ERROR_SIZE 10
429 #define TPM_RET_CODE_IDX 6
430 #define TPM_GET_CAP_RET_SIZE_IDX 10
431 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
432 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
433 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
434 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
435 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
436 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
437 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
438 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
440 #define TPM_CAP_IDX 13
441 #define TPM_CAP_SUBCAP_IDX 21
443 enum tpm_capabilities
{
448 enum tpm_sub_capabilities
{
449 TPM_CAP_PROP_PCR
= 0x1,
450 TPM_CAP_PROP_MANUFACTURER
= 0x3,
451 TPM_CAP_FLAG_PERM
= 0x8,
452 TPM_CAP_FLAG_VOL
= 0x9,
453 TPM_CAP_PROP_OWNER
= 0x11,
454 TPM_CAP_PROP_TIS_TIMEOUT
= 0x15,
455 TPM_CAP_PROP_TIS_DURATION
= 0x20,
459 * This is a semi generic GetCapability command for use
460 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
461 * and their associated sub_capabilities.
464 static const u8 tpm_cap
[] = {
465 0, 193, /* TPM_TAG_RQU_COMMAND */
466 0, 0, 0, 22, /* length */
467 0, 0, 0, 101, /* TPM_ORD_GetCapability */
468 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
469 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
470 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
473 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, u8
*data
, int len
,
478 len
= tpm_transmit(chip
, data
, len
);
481 if (len
== TPM_ERROR_SIZE
) {
482 err
= be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
)));
483 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
489 void tpm_gen_interrupt(struct tpm_chip
*chip
)
491 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
494 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
495 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
496 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
498 rc
= transmit_cmd(chip
, data
, sizeof(data
),
499 "attempting to determine the timeouts");
501 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
503 void tpm_get_timeouts(struct tpm_chip
*chip
)
505 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
509 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
510 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
511 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
513 rc
= transmit_cmd(chip
, data
, sizeof(data
),
514 "attempting to determine the timeouts");
518 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
522 /* Don't overwrite default if value is 0 */
524 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
)));
526 chip
->vendor
.timeout_a
= msecs_to_jiffies(timeout
);
528 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_2_IDX
)));
530 chip
->vendor
.timeout_b
= msecs_to_jiffies(timeout
);
532 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_3_IDX
)));
534 chip
->vendor
.timeout_c
= msecs_to_jiffies(timeout
);
536 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_4_IDX
)));
538 chip
->vendor
.timeout_d
= msecs_to_jiffies(timeout
);
541 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
542 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
543 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_DURATION
;
545 rc
= transmit_cmd(chip
, data
, sizeof(data
),
546 "attempting to determine the durations");
550 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
554 chip
->vendor
.duration
[TPM_SHORT
] =
555 msecs_to_jiffies(be32_to_cpu
556 (*((__be32
*) (data
+
557 TPM_GET_CAP_RET_UINT32_1_IDX
))));
558 chip
->vendor
.duration
[TPM_MEDIUM
] =
559 msecs_to_jiffies(be32_to_cpu
560 (*((__be32
*) (data
+
561 TPM_GET_CAP_RET_UINT32_2_IDX
))));
562 chip
->vendor
.duration
[TPM_LONG
] =
563 msecs_to_jiffies(be32_to_cpu
564 (*((__be32
*) (data
+
565 TPM_GET_CAP_RET_UINT32_3_IDX
))));
567 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
569 void tpm_continue_selftest(struct tpm_chip
*chip
)
572 0, 193, /* TPM_TAG_RQU_COMMAND */
573 0, 0, 0, 10, /* length */
574 0, 0, 0, 83, /* TPM_ORD_GetCapability */
577 tpm_transmit(chip
, data
, sizeof(data
));
579 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
581 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
584 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
587 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
591 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
592 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
593 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
595 rc
= transmit_cmd(chip
, data
, sizeof(data
),
596 "attemtping to determine the permanent state");
599 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_DISABLE_IDX
]);
601 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
603 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
606 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
609 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
613 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
614 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
615 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
617 rc
= transmit_cmd(chip
, data
, sizeof(data
),
618 "attemtping to determine the permanent state");
621 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_INACTIVE_IDX
]);
623 EXPORT_SYMBOL_GPL(tpm_show_active
);
625 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
628 u8 data
[sizeof(tpm_cap
)];
631 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
635 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
636 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
637 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_OWNER
;
639 rc
= transmit_cmd(chip
, data
, sizeof(data
),
640 "attempting to determine the owner state");
643 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_RET_BOOL_1_IDX
]);
645 EXPORT_SYMBOL_GPL(tpm_show_owned
);
647 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
648 struct device_attribute
* attr
, char *buf
)
650 u8 data
[sizeof(tpm_cap
)];
653 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
657 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
658 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
659 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_VOL
;
661 rc
= transmit_cmd(chip
, data
, sizeof(data
),
662 "attempting to determine the temporary state");
665 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_TEMP_INACTIVE_IDX
]);
667 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
669 static const u8 pcrread
[] = {
670 0, 193, /* TPM_TAG_RQU_COMMAND */
671 0, 0, 0, 14, /* length */
672 0, 0, 0, 21, /* TPM_ORD_PcrRead */
673 0, 0, 0, 0 /* PCR index */
676 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
679 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(pcrread
)), 30)];
685 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
689 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
690 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
691 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_PCR
;
693 rc
= transmit_cmd(chip
, data
, sizeof(data
),
694 "attempting to determine the number of PCRS");
698 num_pcrs
= be32_to_cpu(*((__be32
*) (data
+ 14)));
699 for (i
= 0; i
< num_pcrs
; i
++) {
700 memcpy(data
, pcrread
, sizeof(pcrread
));
701 index
= cpu_to_be32(i
);
702 memcpy(data
+ 10, &index
, 4);
703 rc
= transmit_cmd(chip
, data
, sizeof(data
),
704 "attempting to read a PCR");
707 str
+= sprintf(str
, "PCR-%02d: ", i
);
708 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
709 str
+= sprintf(str
, "%02X ", *(data
+ 10 + j
));
710 str
+= sprintf(str
, "\n");
715 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
717 #define READ_PUBEK_RESULT_SIZE 314
718 static const u8 readpubek
[] = {
719 0, 193, /* TPM_TAG_RQU_COMMAND */
720 0, 0, 0, 30, /* length */
721 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
724 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
732 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
736 data
= kzalloc(READ_PUBEK_RESULT_SIZE
, GFP_KERNEL
);
740 memcpy(data
, readpubek
, sizeof(readpubek
));
742 err
= transmit_cmd(chip
, data
, READ_PUBEK_RESULT_SIZE
,
743 "attempting to read the PUBEK");
748 ignore header 10 bytes
749 algorithm 32 bits (1 == RSA )
752 parameters (RSA 12->bytes: keybit, #primes, expbit)
755 ignore checksum 20 bytes
760 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
761 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
762 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
763 "Modulus length: %d\nModulus: \n",
764 data
[10], data
[11], data
[12], data
[13], data
[14],
765 data
[15], data
[16], data
[17], data
[22], data
[23],
766 data
[24], data
[25], data
[26], data
[27], data
[28],
767 data
[29], data
[30], data
[31], data
[32], data
[33],
768 be32_to_cpu(*((__be32
*) (data
+ 34))));
770 for (i
= 0; i
< 256; i
++) {
771 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
772 if ((i
+ 1) % 16 == 0)
773 str
+= sprintf(str
, "\n");
780 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
782 #define CAP_VERSION_1_1 6
783 #define CAP_VERSION_1_2 0x1A
784 #define CAP_VERSION_IDX 13
785 static const u8 cap_version
[] = {
786 0, 193, /* TPM_TAG_RQU_COMMAND */
787 0, 0, 0, 18, /* length */
788 0, 0, 0, 101, /* TPM_ORD_GetCapability */
793 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
796 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
800 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
804 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
805 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
806 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
808 rc
= transmit_cmd(chip
, data
, sizeof(data
),
809 "attempting to determine the manufacturer");
813 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
814 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
816 memcpy(data
, cap_version
, sizeof(cap_version
));
817 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_1
;
818 rc
= transmit_cmd(chip
, data
, sizeof(data
),
819 "attempting to determine the 1.1 version");
824 "TCG version: %d.%d\nFirmware version: %d.%d\n",
825 (int) data
[14], (int) data
[15], (int) data
[16],
831 EXPORT_SYMBOL_GPL(tpm_show_caps
);
833 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
834 struct device_attribute
* attr
, char *buf
)
836 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
840 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
844 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
845 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
846 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
848 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
850 dev_dbg(chip
->dev
, "A TPM error (%d) occurred "
851 "attempting to determine the manufacturer\n",
852 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
856 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
857 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
859 memcpy(data
, cap_version
, sizeof(cap_version
));
860 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_2
;
862 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
864 dev_err(chip
->dev
, "A TPM error (%d) occurred "
865 "attempting to determine the 1.2 version\n",
866 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
870 "TCG version: %d.%d\nFirmware version: %d.%d\n",
871 (int) data
[16], (int) data
[17], (int) data
[18],
877 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
879 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
880 const char *buf
, size_t count
)
882 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
886 chip
->vendor
.cancel(chip
);
889 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
892 * Device file system interface to the TPM
894 int tpm_open(struct inode
*inode
, struct file
*file
)
896 int rc
= 0, minor
= iminor(inode
);
897 struct tpm_chip
*chip
= NULL
, *pos
;
899 spin_lock(&driver_lock
);
901 list_for_each_entry(pos
, &tpm_chip_list
, list
) {
902 if (pos
->vendor
.miscdev
.minor
== minor
) {
913 if (chip
->num_opens
) {
914 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
920 get_device(chip
->dev
);
922 spin_unlock(&driver_lock
);
924 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
925 if (chip
->data_buffer
== NULL
) {
927 put_device(chip
->dev
);
931 atomic_set(&chip
->data_pending
, 0);
933 file
->private_data
= chip
;
937 spin_unlock(&driver_lock
);
940 EXPORT_SYMBOL_GPL(tpm_open
);
942 int tpm_release(struct inode
*inode
, struct file
*file
)
944 struct tpm_chip
*chip
= file
->private_data
;
946 spin_lock(&driver_lock
);
947 file
->private_data
= NULL
;
949 del_singleshot_timer_sync(&chip
->user_read_timer
);
950 flush_scheduled_work();
951 atomic_set(&chip
->data_pending
, 0);
952 put_device(chip
->dev
);
953 kfree(chip
->data_buffer
);
954 spin_unlock(&driver_lock
);
957 EXPORT_SYMBOL_GPL(tpm_release
);
959 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
960 size_t size
, loff_t
*off
)
962 struct tpm_chip
*chip
= file
->private_data
;
963 int in_size
= size
, out_size
;
965 /* cannot perform a write until the read has cleared
966 either via tpm_read or a user_read_timer timeout */
967 while (atomic_read(&chip
->data_pending
) != 0)
970 down(&chip
->buffer_mutex
);
972 if (in_size
> TPM_BUFSIZE
)
973 in_size
= TPM_BUFSIZE
;
976 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
977 up(&chip
->buffer_mutex
);
981 /* atomic tpm command send and result receive */
982 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
984 atomic_set(&chip
->data_pending
, out_size
);
985 up(&chip
->buffer_mutex
);
987 /* Set a timeout by which the reader must come claim the result */
988 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
992 EXPORT_SYMBOL_GPL(tpm_write
);
994 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
995 size_t size
, loff_t
*off
)
997 struct tpm_chip
*chip
= file
->private_data
;
1000 del_singleshot_timer_sync(&chip
->user_read_timer
);
1001 flush_scheduled_work();
1002 ret_size
= atomic_read(&chip
->data_pending
);
1003 atomic_set(&chip
->data_pending
, 0);
1004 if (ret_size
> 0) { /* relay data */
1005 if (size
< ret_size
)
1008 down(&chip
->buffer_mutex
);
1009 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1011 up(&chip
->buffer_mutex
);
1016 EXPORT_SYMBOL_GPL(tpm_read
);
1018 void tpm_remove_hardware(struct device
*dev
)
1020 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1023 dev_err(dev
, "No device data found\n");
1027 spin_lock(&driver_lock
);
1029 list_del(&chip
->list
);
1031 spin_unlock(&driver_lock
);
1033 dev_set_drvdata(dev
, NULL
);
1034 misc_deregister(&chip
->vendor
.miscdev
);
1035 kfree(chip
->vendor
.miscdev
.name
);
1037 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1038 tpm_bios_log_teardown(chip
->bios_dir
);
1040 clear_bit(chip
->dev_num
, dev_mask
);
1046 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1048 static u8 savestate
[] = {
1049 0, 193, /* TPM_TAG_RQU_COMMAND */
1050 0, 0, 0, 10, /* blob length (in bytes) */
1051 0, 0, 0, 152 /* TPM_ORD_SaveState */
1055 * We are about to suspend. Save the TPM state
1056 * so that it can be restored.
1058 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1060 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1064 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1067 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1070 * Resume from a power safe. The BIOS already restored
1073 int tpm_pm_resume(struct device
*dev
)
1075 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1082 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1085 * Called from tpm_<specific>.c probe function only for devices
1086 * the driver has determined it should claim. Prior to calling
1087 * this function the specific probe function has called pci_enable_device
1088 * upon errant exit from this function specific probe function should call
1089 * pci_disable_device
1091 struct tpm_chip
*tpm_register_hardware(struct device
*dev
, const struct tpm_vendor_specific
1094 #define DEVNAME_SIZE 7
1097 struct tpm_chip
*chip
;
1099 /* Driver specific per-device data */
1100 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1104 init_MUTEX(&chip
->buffer_mutex
);
1105 init_MUTEX(&chip
->tpm_mutex
);
1106 INIT_LIST_HEAD(&chip
->list
);
1108 INIT_WORK(&chip
->work
, timeout_work
);
1110 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1111 (unsigned long)chip
);
1113 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1115 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1117 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1118 dev_err(dev
, "No available tpm device numbers\n");
1121 } else if (chip
->dev_num
== 0)
1122 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1124 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1126 set_bit(chip
->dev_num
, dev_mask
);
1128 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1129 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1130 chip
->vendor
.miscdev
.name
= devname
;
1132 chip
->vendor
.miscdev
.parent
= dev
;
1133 chip
->dev
= get_device(dev
);
1135 if (misc_register(&chip
->vendor
.miscdev
)) {
1137 "unable to misc_register %s, minor %d\n",
1138 chip
->vendor
.miscdev
.name
,
1139 chip
->vendor
.miscdev
.minor
);
1141 clear_bit(chip
->dev_num
, dev_mask
);
1147 spin_lock(&driver_lock
);
1149 dev_set_drvdata(dev
, chip
);
1151 list_add(&chip
->list
, &tpm_chip_list
);
1153 spin_unlock(&driver_lock
);
1155 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1156 list_del(&chip
->list
);
1157 misc_deregister(&chip
->vendor
.miscdev
);
1159 clear_bit(chip
->dev_num
, dev_mask
);
1165 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1169 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1171 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1172 MODULE_DESCRIPTION("TPM Driver");
1173 MODULE_VERSION("2.0");
1174 MODULE_LICENSE("GPL");