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
= msecs_to_jiffies(timeout
);
530 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_2_IDX
)));
532 chip
->vendor
.timeout_b
= msecs_to_jiffies(timeout
);
534 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_3_IDX
)));
536 chip
->vendor
.timeout_c
= msecs_to_jiffies(timeout
);
538 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_4_IDX
)));
540 chip
->vendor
.timeout_d
= msecs_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 msecs_to_jiffies(be32_to_cpu
558 (*((__be32
*) (data
+
559 TPM_GET_CAP_RET_UINT32_1_IDX
))));
560 chip
->vendor
.duration
[TPM_MEDIUM
] =
561 msecs_to_jiffies(be32_to_cpu
562 (*((__be32
*) (data
+
563 TPM_GET_CAP_RET_UINT32_2_IDX
))));
564 chip
->vendor
.duration
[TPM_LONG
] =
565 msecs_to_jiffies(be32_to_cpu
566 (*((__be32
*) (data
+
567 TPM_GET_CAP_RET_UINT32_3_IDX
))));
569 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
571 void tpm_continue_selftest(struct tpm_chip
*chip
)
574 0, 193, /* TPM_TAG_RQU_COMMAND */
575 0, 0, 0, 10, /* length */
576 0, 0, 0, 83, /* TPM_ORD_GetCapability */
579 tpm_transmit(chip
, data
, sizeof(data
));
581 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
583 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
586 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
589 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
593 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
594 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
595 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
597 rc
= transmit_cmd(chip
, data
, sizeof(data
),
598 "attemtping to determine the permanent state");
601 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_DISABLE_IDX
]);
603 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
605 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
608 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
611 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
615 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
616 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
617 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
619 rc
= transmit_cmd(chip
, data
, sizeof(data
),
620 "attemtping to determine the permanent state");
623 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_INACTIVE_IDX
]);
625 EXPORT_SYMBOL_GPL(tpm_show_active
);
627 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
630 u8 data
[sizeof(tpm_cap
)];
633 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
637 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
638 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
639 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_OWNER
;
641 rc
= transmit_cmd(chip
, data
, sizeof(data
),
642 "attempting to determine the owner state");
645 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_RET_BOOL_1_IDX
]);
647 EXPORT_SYMBOL_GPL(tpm_show_owned
);
649 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
650 struct device_attribute
* attr
, char *buf
)
652 u8 data
[sizeof(tpm_cap
)];
655 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
659 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
660 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
661 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_VOL
;
663 rc
= transmit_cmd(chip
, data
, sizeof(data
),
664 "attempting to determine the temporary state");
667 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_TEMP_INACTIVE_IDX
]);
669 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
671 static const u8 pcrread
[] = {
672 0, 193, /* TPM_TAG_RQU_COMMAND */
673 0, 0, 0, 14, /* length */
674 0, 0, 0, 21, /* TPM_ORD_PcrRead */
675 0, 0, 0, 0 /* PCR index */
678 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
681 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(pcrread
)), 30)];
687 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
691 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
692 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
693 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_PCR
;
695 rc
= transmit_cmd(chip
, data
, sizeof(data
),
696 "attempting to determine the number of PCRS");
700 num_pcrs
= be32_to_cpu(*((__be32
*) (data
+ 14)));
701 for (i
= 0; i
< num_pcrs
; i
++) {
702 memcpy(data
, pcrread
, sizeof(pcrread
));
703 index
= cpu_to_be32(i
);
704 memcpy(data
+ 10, &index
, 4);
705 rc
= transmit_cmd(chip
, data
, sizeof(data
),
706 "attempting to read a PCR");
709 str
+= sprintf(str
, "PCR-%02d: ", i
);
710 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
711 str
+= sprintf(str
, "%02X ", *(data
+ 10 + j
));
712 str
+= sprintf(str
, "\n");
717 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
719 #define READ_PUBEK_RESULT_SIZE 314
720 static const u8 readpubek
[] = {
721 0, 193, /* TPM_TAG_RQU_COMMAND */
722 0, 0, 0, 30, /* length */
723 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
726 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
734 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
738 data
= kzalloc(READ_PUBEK_RESULT_SIZE
, GFP_KERNEL
);
742 memcpy(data
, readpubek
, sizeof(readpubek
));
744 err
= transmit_cmd(chip
, data
, READ_PUBEK_RESULT_SIZE
,
745 "attempting to read the PUBEK");
750 ignore header 10 bytes
751 algorithm 32 bits (1 == RSA )
754 parameters (RSA 12->bytes: keybit, #primes, expbit)
757 ignore checksum 20 bytes
762 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
763 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
764 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
765 "Modulus length: %d\nModulus: \n",
766 data
[10], data
[11], data
[12], data
[13], data
[14],
767 data
[15], data
[16], data
[17], data
[22], data
[23],
768 data
[24], data
[25], data
[26], data
[27], data
[28],
769 data
[29], data
[30], data
[31], data
[32], data
[33],
770 be32_to_cpu(*((__be32
*) (data
+ 34))));
772 for (i
= 0; i
< 256; i
++) {
773 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
774 if ((i
+ 1) % 16 == 0)
775 str
+= sprintf(str
, "\n");
782 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
784 #define CAP_VERSION_1_1 6
785 #define CAP_VERSION_1_2 0x1A
786 #define CAP_VERSION_IDX 13
787 static const u8 cap_version
[] = {
788 0, 193, /* TPM_TAG_RQU_COMMAND */
789 0, 0, 0, 18, /* length */
790 0, 0, 0, 101, /* TPM_ORD_GetCapability */
795 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
798 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
802 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
806 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
807 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
808 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
810 rc
= transmit_cmd(chip
, data
, sizeof(data
),
811 "attempting to determine the manufacturer");
815 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
816 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
818 memcpy(data
, cap_version
, sizeof(cap_version
));
819 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_1
;
820 rc
= transmit_cmd(chip
, data
, sizeof(data
),
821 "attempting to determine the 1.1 version");
826 "TCG version: %d.%d\nFirmware version: %d.%d\n",
827 (int) data
[14], (int) data
[15], (int) data
[16],
833 EXPORT_SYMBOL_GPL(tpm_show_caps
);
835 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
836 struct device_attribute
* attr
, char *buf
)
838 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
842 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
846 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
847 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
848 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
850 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
852 dev_dbg(chip
->dev
, "A TPM error (%d) occurred "
853 "attempting to determine the manufacturer\n",
854 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
858 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
859 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
861 memcpy(data
, cap_version
, sizeof(cap_version
));
862 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_2
;
864 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
866 dev_err(chip
->dev
, "A TPM error (%d) occurred "
867 "attempting to determine the 1.2 version\n",
868 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
872 "TCG version: %d.%d\nFirmware version: %d.%d\n",
873 (int) data
[16], (int) data
[17], (int) data
[18],
879 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
881 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
882 const char *buf
, size_t count
)
884 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
888 chip
->vendor
.cancel(chip
);
891 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
894 * Device file system interface to the TPM
896 int tpm_open(struct inode
*inode
, struct file
*file
)
898 int rc
= 0, minor
= iminor(inode
);
899 struct tpm_chip
*chip
= NULL
, *pos
;
902 spin_lock(&driver_lock
);
904 list_for_each_entry(pos
, &tpm_chip_list
, list
) {
905 if (pos
->vendor
.miscdev
.minor
== minor
) {
916 if (chip
->num_opens
) {
917 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
923 get_device(chip
->dev
);
925 spin_unlock(&driver_lock
);
927 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
928 if (chip
->data_buffer
== NULL
) {
930 put_device(chip
->dev
);
935 atomic_set(&chip
->data_pending
, 0);
937 file
->private_data
= chip
;
942 spin_unlock(&driver_lock
);
946 EXPORT_SYMBOL_GPL(tpm_open
);
948 int tpm_release(struct inode
*inode
, struct file
*file
)
950 struct tpm_chip
*chip
= file
->private_data
;
952 flush_scheduled_work();
953 spin_lock(&driver_lock
);
954 file
->private_data
= NULL
;
955 del_singleshot_timer_sync(&chip
->user_read_timer
);
956 atomic_set(&chip
->data_pending
, 0);
958 put_device(chip
->dev
);
959 kfree(chip
->data_buffer
);
960 spin_unlock(&driver_lock
);
963 EXPORT_SYMBOL_GPL(tpm_release
);
965 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
966 size_t size
, loff_t
*off
)
968 struct tpm_chip
*chip
= file
->private_data
;
969 int in_size
= size
, out_size
;
971 /* cannot perform a write until the read has cleared
972 either via tpm_read or a user_read_timer timeout */
973 while (atomic_read(&chip
->data_pending
) != 0)
976 mutex_lock(&chip
->buffer_mutex
);
978 if (in_size
> TPM_BUFSIZE
)
979 in_size
= TPM_BUFSIZE
;
982 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
983 mutex_unlock(&chip
->buffer_mutex
);
987 /* atomic tpm command send and result receive */
988 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
990 atomic_set(&chip
->data_pending
, out_size
);
991 mutex_unlock(&chip
->buffer_mutex
);
993 /* Set a timeout by which the reader must come claim the result */
994 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
998 EXPORT_SYMBOL_GPL(tpm_write
);
1000 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
1001 size_t size
, loff_t
*off
)
1003 struct tpm_chip
*chip
= file
->private_data
;
1006 del_singleshot_timer_sync(&chip
->user_read_timer
);
1007 flush_scheduled_work();
1008 ret_size
= atomic_read(&chip
->data_pending
);
1009 atomic_set(&chip
->data_pending
, 0);
1010 if (ret_size
> 0) { /* relay data */
1011 if (size
< ret_size
)
1014 mutex_lock(&chip
->buffer_mutex
);
1015 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1017 mutex_unlock(&chip
->buffer_mutex
);
1022 EXPORT_SYMBOL_GPL(tpm_read
);
1024 void tpm_remove_hardware(struct device
*dev
)
1026 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1029 dev_err(dev
, "No device data found\n");
1033 spin_lock(&driver_lock
);
1035 list_del(&chip
->list
);
1037 spin_unlock(&driver_lock
);
1039 misc_deregister(&chip
->vendor
.miscdev
);
1041 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1042 tpm_bios_log_teardown(chip
->bios_dir
);
1044 /* write it this way to be explicit (chip->dev == dev) */
1045 put_device(chip
->dev
);
1047 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1050 * We are about to suspend. Save the TPM state
1051 * so that it can be restored.
1053 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1055 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1057 0, 193, /* TPM_TAG_RQU_COMMAND */
1058 0, 0, 0, 10, /* blob length (in bytes) */
1059 0, 0, 0, 152 /* TPM_ORD_SaveState */
1065 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1068 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1071 * Resume from a power safe. The BIOS already restored
1074 int tpm_pm_resume(struct device
*dev
)
1076 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1083 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1086 * Once all references to platform device are down to 0,
1087 * release all allocated structures.
1088 * In case vendor provided release function,
1091 static void tpm_dev_release(struct device
*dev
)
1093 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1095 if (chip
->vendor
.release
)
1096 chip
->vendor
.release(dev
);
1100 clear_bit(chip
->dev_num
, dev_mask
);
1101 kfree(chip
->vendor
.miscdev
.name
);
1106 * Called from tpm_<specific>.c probe function only for devices
1107 * the driver has determined it should claim. Prior to calling
1108 * this function the specific probe function has called pci_enable_device
1109 * upon errant exit from this function specific probe function should call
1110 * pci_disable_device
1112 struct tpm_chip
*tpm_register_hardware(struct device
*dev
, const struct tpm_vendor_specific
1115 #define DEVNAME_SIZE 7
1118 struct tpm_chip
*chip
;
1120 /* Driver specific per-device data */
1121 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1122 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1124 if (chip
== NULL
|| devname
== NULL
) {
1130 mutex_init(&chip
->buffer_mutex
);
1131 mutex_init(&chip
->tpm_mutex
);
1132 INIT_LIST_HEAD(&chip
->list
);
1134 INIT_WORK(&chip
->work
, timeout_work
);
1136 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1137 (unsigned long)chip
);
1139 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1141 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1143 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1144 dev_err(dev
, "No available tpm device numbers\n");
1147 } else if (chip
->dev_num
== 0)
1148 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1150 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1152 set_bit(chip
->dev_num
, dev_mask
);
1154 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1155 chip
->vendor
.miscdev
.name
= devname
;
1157 chip
->vendor
.miscdev
.parent
= dev
;
1158 chip
->dev
= get_device(dev
);
1159 chip
->release
= dev
->release
;
1160 dev
->release
= tpm_dev_release
;
1161 dev_set_drvdata(dev
, chip
);
1163 if (misc_register(&chip
->vendor
.miscdev
)) {
1165 "unable to misc_register %s, minor %d\n",
1166 chip
->vendor
.miscdev
.name
,
1167 chip
->vendor
.miscdev
.minor
);
1168 put_device(chip
->dev
);
1172 spin_lock(&driver_lock
);
1174 list_add(&chip
->list
, &tpm_chip_list
);
1176 spin_unlock(&driver_lock
);
1178 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1179 list_del(&chip
->list
);
1180 misc_deregister(&chip
->vendor
.miscdev
);
1181 put_device(chip
->dev
);
1185 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1189 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1191 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1192 MODULE_DESCRIPTION("TPM Driver");
1193 MODULE_VERSION("2.0");
1194 MODULE_LICENSE("GPL");