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>
29 #include <linux/smp_lock.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
50 static LIST_HEAD(tpm_chip_list
);
51 static DEFINE_SPINLOCK(driver_lock
);
52 static DECLARE_BITMAP(dev_mask
, TPM_NUM_DEVICES
);
55 * Array with one entry per ordinal defining the maximum amount
56 * of time the chip could take to return the result. The ordinal
57 * designation of short, medium or long is defined in a table in
58 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
59 * values of the SHORT, MEDIUM, and LONG durations are retrieved
60 * from the chip during initialization with a call to tpm_get_timeouts.
62 static const u8 tpm_protected_ordinal_duration
[TPM_MAX_PROTECTED_ORDINAL
] = {
63 TPM_UNDEFINED
, /* 0 */
68 TPM_UNDEFINED
, /* 5 */
77 static const u8 tpm_ordinal_duration
[TPM_MAX_ORDINAL
] = {
78 TPM_UNDEFINED
, /* 0 */
83 TPM_UNDEFINED
, /* 5 */
133 TPM_UNDEFINED
, /* 55 */
153 TPM_UNDEFINED
, /* 75 */
163 TPM_UNDEFINED
, /* 85 */
173 TPM_UNDEFINED
, /* 95 */
178 TPM_MEDIUM
, /* 100 */
183 TPM_UNDEFINED
, /* 105 */
213 TPM_UNDEFINED
, /* 135 */
223 TPM_UNDEFINED
, /* 145 */
233 TPM_UNDEFINED
, /* 155 */
243 TPM_UNDEFINED
, /* 165 */
253 TPM_UNDEFINED
, /* 175 */
258 TPM_MEDIUM
, /* 180 */
263 TPM_MEDIUM
, /* 185 */
268 TPM_UNDEFINED
, /* 190 */
273 TPM_UNDEFINED
, /* 195 */
288 TPM_MEDIUM
, /* 210 */
293 TPM_UNDEFINED
, /* 215 */
303 TPM_UNDEFINED
, /* 225 */
313 TPM_UNDEFINED
, /* 235 */
323 static void user_reader_timeout(unsigned long ptr
)
325 struct tpm_chip
*chip
= (struct tpm_chip
*) ptr
;
327 schedule_work(&chip
->work
);
330 static void timeout_work(struct work_struct
*work
)
332 struct tpm_chip
*chip
= container_of(work
, struct tpm_chip
, work
);
334 mutex_lock(&chip
->buffer_mutex
);
335 atomic_set(&chip
->data_pending
, 0);
336 memset(chip
->data_buffer
, 0, TPM_BUFSIZE
);
337 mutex_unlock(&chip
->buffer_mutex
);
341 * Returns max number of jiffies to wait
343 unsigned long tpm_calc_ordinal_duration(struct tpm_chip
*chip
,
346 int duration_idx
= TPM_UNDEFINED
;
349 if (ordinal
< TPM_MAX_ORDINAL
)
350 duration_idx
= tpm_ordinal_duration
[ordinal
];
351 else if ((ordinal
& TPM_PROTECTED_ORDINAL_MASK
) <
352 TPM_MAX_PROTECTED_ORDINAL
)
354 tpm_protected_ordinal_duration
[ordinal
&
355 TPM_PROTECTED_ORDINAL_MASK
];
357 if (duration_idx
!= TPM_UNDEFINED
)
358 duration
= chip
->vendor
.duration
[duration_idx
];
364 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration
);
367 * Internal kernel interface to transmit TPM commands
369 static ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
376 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
377 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
380 if (count
> bufsiz
) {
382 "invalid count value %x %zx \n", count
, bufsiz
);
386 mutex_lock(&chip
->tpm_mutex
);
388 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
390 "tpm_transmit: tpm_send: error %zd\n", rc
);
394 if (chip
->vendor
.irq
)
397 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
399 u8 status
= chip
->vendor
.status(chip
);
400 if ((status
& chip
->vendor
.req_complete_mask
) ==
401 chip
->vendor
.req_complete_val
)
404 if ((status
== chip
->vendor
.req_canceled
)) {
405 dev_err(chip
->dev
, "Operation Canceled\n");
410 msleep(TPM_TIMEOUT
); /* CHECK */
412 } while (time_before(jiffies
, stop
));
414 chip
->vendor
.cancel(chip
);
415 dev_err(chip
->dev
, "Operation Timed out\n");
420 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
423 "tpm_transmit: tpm_recv: error %zd\n", rc
);
425 mutex_unlock(&chip
->tpm_mutex
);
429 #define TPM_DIGEST_SIZE 20
430 #define TPM_ERROR_SIZE 10
431 #define TPM_RET_CODE_IDX 6
432 #define TPM_GET_CAP_RET_SIZE_IDX 10
433 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
434 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
435 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
436 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
437 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
438 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
439 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
440 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
442 #define TPM_CAP_IDX 13
443 #define TPM_CAP_SUBCAP_IDX 21
445 enum tpm_capabilities
{
450 enum tpm_sub_capabilities
{
451 TPM_CAP_PROP_PCR
= 0x1,
452 TPM_CAP_PROP_MANUFACTURER
= 0x3,
453 TPM_CAP_FLAG_PERM
= 0x8,
454 TPM_CAP_FLAG_VOL
= 0x9,
455 TPM_CAP_PROP_OWNER
= 0x11,
456 TPM_CAP_PROP_TIS_TIMEOUT
= 0x15,
457 TPM_CAP_PROP_TIS_DURATION
= 0x20,
461 * This is a semi generic GetCapability command for use
462 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
463 * and their associated sub_capabilities.
466 static const u8 tpm_cap
[] = {
467 0, 193, /* TPM_TAG_RQU_COMMAND */
468 0, 0, 0, 22, /* length */
469 0, 0, 0, 101, /* TPM_ORD_GetCapability */
470 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
471 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
472 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
475 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, u8
*data
, int len
,
480 len
= tpm_transmit(chip
, data
, len
);
483 if (len
== TPM_ERROR_SIZE
) {
484 err
= be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
)));
485 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
491 void tpm_gen_interrupt(struct tpm_chip
*chip
)
493 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
496 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
497 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
498 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
500 rc
= transmit_cmd(chip
, data
, sizeof(data
),
501 "attempting to determine the timeouts");
503 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
505 void tpm_get_timeouts(struct tpm_chip
*chip
)
507 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
511 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
512 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
513 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
515 rc
= transmit_cmd(chip
, data
, sizeof(data
),
516 "attempting to determine the timeouts");
520 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
524 /* Don't overwrite default if value is 0 */
526 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
)));
528 chip
->vendor
.timeout_a
= usecs_to_jiffies(timeout
);
530 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_2_IDX
)));
532 chip
->vendor
.timeout_b
= usecs_to_jiffies(timeout
);
534 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_3_IDX
)));
536 chip
->vendor
.timeout_c
= usecs_to_jiffies(timeout
);
538 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_4_IDX
)));
540 chip
->vendor
.timeout_d
= usecs_to_jiffies(timeout
);
543 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
544 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
545 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_DURATION
;
547 rc
= transmit_cmd(chip
, data
, sizeof(data
),
548 "attempting to determine the durations");
552 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
556 chip
->vendor
.duration
[TPM_SHORT
] =
557 usecs_to_jiffies(be32_to_cpu
558 (*((__be32
*) (data
+
559 TPM_GET_CAP_RET_UINT32_1_IDX
))));
560 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
561 * value wrong and apparently reports msecs rather than usecs. So we
562 * fix up the resulting too-small TPM_SHORT value to make things work.
564 if (chip
->vendor
.duration
[TPM_SHORT
] < (HZ
/100))
565 chip
->vendor
.duration
[TPM_SHORT
] = HZ
;
567 chip
->vendor
.duration
[TPM_MEDIUM
] =
568 usecs_to_jiffies(be32_to_cpu
569 (*((__be32
*) (data
+
570 TPM_GET_CAP_RET_UINT32_2_IDX
))));
571 chip
->vendor
.duration
[TPM_LONG
] =
572 usecs_to_jiffies(be32_to_cpu
573 (*((__be32
*) (data
+
574 TPM_GET_CAP_RET_UINT32_3_IDX
))));
576 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
578 void tpm_continue_selftest(struct tpm_chip
*chip
)
581 0, 193, /* TPM_TAG_RQU_COMMAND */
582 0, 0, 0, 10, /* length */
583 0, 0, 0, 83, /* TPM_ORD_GetCapability */
586 tpm_transmit(chip
, data
, sizeof(data
));
588 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
590 #define TPM_INTERNAL_RESULT_SIZE 200
592 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
598 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
602 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
606 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
607 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
608 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
610 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
611 "attemtping to determine the permanent enabled state");
617 rc
= sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_DISABLE_IDX
]);
622 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
624 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
630 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
634 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
638 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
639 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
640 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
642 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
643 "attemtping to determine the permanent active state");
649 rc
= sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_INACTIVE_IDX
]);
654 EXPORT_SYMBOL_GPL(tpm_show_active
);
656 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
662 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
666 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
670 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
671 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
672 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_OWNER
;
674 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
675 "attempting to determine the owner state");
681 rc
= sprintf(buf
, "%d\n", data
[TPM_GET_CAP_RET_BOOL_1_IDX
]);
686 EXPORT_SYMBOL_GPL(tpm_show_owned
);
688 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
689 struct device_attribute
* attr
, char *buf
)
694 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
698 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
702 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
703 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
704 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_VOL
;
706 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
707 "attempting to determine the temporary state");
713 rc
= sprintf(buf
, "%d\n", data
[TPM_GET_CAP_TEMP_INACTIVE_IDX
]);
718 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
720 static const u8 pcrread
[] = {
721 0, 193, /* TPM_TAG_RQU_COMMAND */
722 0, 0, 0, 14, /* length */
723 0, 0, 0, 21, /* TPM_ORD_PcrRead */
724 0, 0, 0, 0 /* PCR index */
727 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
736 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
740 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
744 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
745 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
746 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_PCR
;
748 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
749 "attempting to determine the number of PCRS");
755 num_pcrs
= be32_to_cpu(*((__be32
*) (data
+ 14)));
756 for (i
= 0; i
< num_pcrs
; i
++) {
757 memcpy(data
, pcrread
, sizeof(pcrread
));
758 index
= cpu_to_be32(i
);
759 memcpy(data
+ 10, &index
, 4);
760 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
761 "attempting to read a PCR");
764 str
+= sprintf(str
, "PCR-%02d: ", i
);
765 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
766 str
+= sprintf(str
, "%02X ", *(data
+ 10 + j
));
767 str
+= sprintf(str
, "\n");
773 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
775 #define READ_PUBEK_RESULT_SIZE 314
776 static const u8 readpubek
[] = {
777 0, 193, /* TPM_TAG_RQU_COMMAND */
778 0, 0, 0, 30, /* length */
779 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
782 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
790 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
794 data
= kzalloc(READ_PUBEK_RESULT_SIZE
, GFP_KERNEL
);
798 memcpy(data
, readpubek
, sizeof(readpubek
));
800 err
= transmit_cmd(chip
, data
, READ_PUBEK_RESULT_SIZE
,
801 "attempting to read the PUBEK");
806 ignore header 10 bytes
807 algorithm 32 bits (1 == RSA )
810 parameters (RSA 12->bytes: keybit, #primes, expbit)
813 ignore checksum 20 bytes
818 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
819 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
820 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
821 "Modulus length: %d\nModulus: \n",
822 data
[10], data
[11], data
[12], data
[13], data
[14],
823 data
[15], data
[16], data
[17], data
[22], data
[23],
824 data
[24], data
[25], data
[26], data
[27], data
[28],
825 data
[29], data
[30], data
[31], data
[32], data
[33],
826 be32_to_cpu(*((__be32
*) (data
+ 34))));
828 for (i
= 0; i
< 256; i
++) {
829 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
830 if ((i
+ 1) % 16 == 0)
831 str
+= sprintf(str
, "\n");
838 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
840 #define CAP_VERSION_1_1 6
841 #define CAP_VERSION_1_2 0x1A
842 #define CAP_VERSION_IDX 13
843 static const u8 cap_version
[] = {
844 0, 193, /* TPM_TAG_RQU_COMMAND */
845 0, 0, 0, 18, /* length */
846 0, 0, 0, 101, /* TPM_ORD_GetCapability */
851 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
858 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
862 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
866 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
867 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
868 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
870 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
871 "attempting to determine the manufacturer");
877 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
878 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
880 memcpy(data
, cap_version
, sizeof(cap_version
));
881 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_1
;
882 rc
= transmit_cmd(chip
, data
, TPM_INTERNAL_RESULT_SIZE
,
883 "attempting to determine the 1.1 version");
888 "TCG version: %d.%d\nFirmware version: %d.%d\n",
889 (int) data
[14], (int) data
[15], (int) data
[16],
896 EXPORT_SYMBOL_GPL(tpm_show_caps
);
898 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
899 struct device_attribute
* attr
, char *buf
)
905 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
909 data
= kzalloc(TPM_INTERNAL_RESULT_SIZE
, GFP_KERNEL
);
913 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
914 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
915 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
917 len
= tpm_transmit(chip
, data
, TPM_INTERNAL_RESULT_SIZE
);
918 if (len
<= TPM_ERROR_SIZE
) {
919 dev_dbg(chip
->dev
, "A TPM error (%d) occurred "
920 "attempting to determine the manufacturer\n",
921 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
926 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
927 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
929 memcpy(data
, cap_version
, sizeof(cap_version
));
930 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_2
;
932 len
= tpm_transmit(chip
, data
, TPM_INTERNAL_RESULT_SIZE
);
933 if (len
<= TPM_ERROR_SIZE
) {
934 dev_err(chip
->dev
, "A TPM error (%d) occurred "
935 "attempting to determine the 1.2 version\n",
936 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
940 "TCG version: %d.%d\nFirmware version: %d.%d\n",
941 (int) data
[16], (int) data
[17], (int) data
[18],
948 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
950 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
951 const char *buf
, size_t count
)
953 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
957 chip
->vendor
.cancel(chip
);
960 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
963 * Device file system interface to the TPM
965 * It's assured that the chip will be opened just once,
966 * by the check of is_open variable, which is protected
969 int tpm_open(struct inode
*inode
, struct file
*file
)
971 int minor
= iminor(inode
);
972 struct tpm_chip
*chip
= NULL
, *pos
;
975 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
976 if (pos
->vendor
.miscdev
.minor
== minor
) {
978 get_device(chip
->dev
);
987 if (test_and_set_bit(0, &chip
->is_open
)) {
988 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
989 put_device(chip
->dev
);
993 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
994 if (chip
->data_buffer
== NULL
) {
995 clear_bit(0, &chip
->is_open
);
996 put_device(chip
->dev
);
1000 atomic_set(&chip
->data_pending
, 0);
1002 file
->private_data
= chip
;
1005 EXPORT_SYMBOL_GPL(tpm_open
);
1008 * Called on file close
1010 int tpm_release(struct inode
*inode
, struct file
*file
)
1012 struct tpm_chip
*chip
= file
->private_data
;
1014 del_singleshot_timer_sync(&chip
->user_read_timer
);
1015 flush_scheduled_work();
1016 file
->private_data
= NULL
;
1017 atomic_set(&chip
->data_pending
, 0);
1018 kfree(chip
->data_buffer
);
1019 clear_bit(0, &chip
->is_open
);
1020 put_device(chip
->dev
);
1023 EXPORT_SYMBOL_GPL(tpm_release
);
1025 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
1026 size_t size
, loff_t
*off
)
1028 struct tpm_chip
*chip
= file
->private_data
;
1029 size_t in_size
= size
, out_size
;
1031 /* cannot perform a write until the read has cleared
1032 either via tpm_read or a user_read_timer timeout */
1033 while (atomic_read(&chip
->data_pending
) != 0)
1034 msleep(TPM_TIMEOUT
);
1036 mutex_lock(&chip
->buffer_mutex
);
1038 if (in_size
> TPM_BUFSIZE
)
1039 in_size
= TPM_BUFSIZE
;
1042 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
1043 mutex_unlock(&chip
->buffer_mutex
);
1047 /* atomic tpm command send and result receive */
1048 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
1050 atomic_set(&chip
->data_pending
, out_size
);
1051 mutex_unlock(&chip
->buffer_mutex
);
1053 /* Set a timeout by which the reader must come claim the result */
1054 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
1058 EXPORT_SYMBOL_GPL(tpm_write
);
1060 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
1061 size_t size
, loff_t
*off
)
1063 struct tpm_chip
*chip
= file
->private_data
;
1066 del_singleshot_timer_sync(&chip
->user_read_timer
);
1067 flush_scheduled_work();
1068 ret_size
= atomic_read(&chip
->data_pending
);
1069 atomic_set(&chip
->data_pending
, 0);
1070 if (ret_size
> 0) { /* relay data */
1071 if (size
< ret_size
)
1074 mutex_lock(&chip
->buffer_mutex
);
1075 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1077 mutex_unlock(&chip
->buffer_mutex
);
1082 EXPORT_SYMBOL_GPL(tpm_read
);
1084 void tpm_remove_hardware(struct device
*dev
)
1086 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1089 dev_err(dev
, "No device data found\n");
1093 spin_lock(&driver_lock
);
1094 list_del_rcu(&chip
->list
);
1095 spin_unlock(&driver_lock
);
1098 misc_deregister(&chip
->vendor
.miscdev
);
1099 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1100 tpm_bios_log_teardown(chip
->bios_dir
);
1102 /* write it this way to be explicit (chip->dev == dev) */
1103 put_device(chip
->dev
);
1105 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1108 * We are about to suspend. Save the TPM state
1109 * so that it can be restored.
1111 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1113 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1115 0, 193, /* TPM_TAG_RQU_COMMAND */
1116 0, 0, 0, 10, /* blob length (in bytes) */
1117 0, 0, 0, 152 /* TPM_ORD_SaveState */
1123 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1126 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1129 * Resume from a power safe. The BIOS already restored
1132 int tpm_pm_resume(struct device
*dev
)
1134 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1141 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1143 /* In case vendor provided release function, call it too.*/
1145 void tpm_dev_vendor_release(struct tpm_chip
*chip
)
1147 if (chip
->vendor
.release
)
1148 chip
->vendor
.release(chip
->dev
);
1150 clear_bit(chip
->dev_num
, dev_mask
);
1151 kfree(chip
->vendor
.miscdev
.name
);
1153 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release
);
1157 * Once all references to platform device are down to 0,
1158 * release all allocated structures.
1160 void tpm_dev_release(struct device
*dev
)
1162 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1164 tpm_dev_vendor_release(chip
);
1169 EXPORT_SYMBOL_GPL(tpm_dev_release
);
1172 * Called from tpm_<specific>.c probe function only for devices
1173 * the driver has determined it should claim. Prior to calling
1174 * this function the specific probe function has called pci_enable_device
1175 * upon errant exit from this function specific probe function should call
1176 * pci_disable_device
1178 struct tpm_chip
*tpm_register_hardware(struct device
*dev
,
1179 const struct tpm_vendor_specific
*entry
)
1181 #define DEVNAME_SIZE 7
1184 struct tpm_chip
*chip
;
1186 /* Driver specific per-device data */
1187 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1188 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1190 if (chip
== NULL
|| devname
== NULL
)
1193 mutex_init(&chip
->buffer_mutex
);
1194 mutex_init(&chip
->tpm_mutex
);
1195 INIT_LIST_HEAD(&chip
->list
);
1197 INIT_WORK(&chip
->work
, timeout_work
);
1199 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1200 (unsigned long)chip
);
1202 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1204 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1206 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1207 dev_err(dev
, "No available tpm device numbers\n");
1209 } else if (chip
->dev_num
== 0)
1210 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1212 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1214 set_bit(chip
->dev_num
, dev_mask
);
1216 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1217 chip
->vendor
.miscdev
.name
= devname
;
1219 chip
->vendor
.miscdev
.parent
= dev
;
1220 chip
->dev
= get_device(dev
);
1221 chip
->release
= dev
->release
;
1222 dev
->release
= tpm_dev_release
;
1223 dev_set_drvdata(dev
, chip
);
1225 if (misc_register(&chip
->vendor
.miscdev
)) {
1227 "unable to misc_register %s, minor %d\n",
1228 chip
->vendor
.miscdev
.name
,
1229 chip
->vendor
.miscdev
.minor
);
1230 put_device(chip
->dev
);
1234 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1235 misc_deregister(&chip
->vendor
.miscdev
);
1236 put_device(chip
->dev
);
1241 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1243 /* Make chip available */
1244 spin_lock(&driver_lock
);
1245 list_add_rcu(&chip
->list
, &tpm_chip_list
);
1246 spin_unlock(&driver_lock
);
1255 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1257 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1258 MODULE_DESCRIPTION("TPM Driver");
1259 MODULE_VERSION("2.0");
1260 MODULE_LICENSE("GPL");