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/spinlock.h>
31 TPM_MINOR
= 224, /* officially assigned */
33 TPM_NUM_DEVICES
= 256,
43 #define TPM_MAX_ORDINAL 243
44 #define TPM_MAX_PROTECTED_ORDINAL 12
45 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
47 static LIST_HEAD(tpm_chip_list
);
48 static DEFINE_SPINLOCK(driver_lock
);
49 static DECLARE_BITMAP(dev_mask
, TPM_NUM_DEVICES
);
52 * Array with one entry per ordinal defining the maximum amount
53 * of time the chip could take to return the result. The ordinal
54 * designation of short, medium or long is defined in a table in
55 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
56 * values of the SHORT, MEDIUM, and LONG durations are retrieved
57 * from the chip during initialization with a call to tpm_get_timeouts.
59 static const u8 tpm_protected_ordinal_duration
[TPM_MAX_PROTECTED_ORDINAL
] = {
60 TPM_UNDEFINED
, /* 0 */
65 TPM_UNDEFINED
, /* 5 */
74 static const u8 tpm_ordinal_duration
[TPM_MAX_ORDINAL
] = {
75 TPM_UNDEFINED
, /* 0 */
80 TPM_UNDEFINED
, /* 5 */
130 TPM_UNDEFINED
, /* 55 */
150 TPM_UNDEFINED
, /* 75 */
160 TPM_UNDEFINED
, /* 85 */
170 TPM_UNDEFINED
, /* 95 */
175 TPM_MEDIUM
, /* 100 */
180 TPM_UNDEFINED
, /* 105 */
210 TPM_UNDEFINED
, /* 135 */
220 TPM_UNDEFINED
, /* 145 */
230 TPM_UNDEFINED
, /* 155 */
240 TPM_UNDEFINED
, /* 165 */
250 TPM_UNDEFINED
, /* 175 */
255 TPM_MEDIUM
, /* 180 */
260 TPM_MEDIUM
, /* 185 */
265 TPM_UNDEFINED
, /* 190 */
270 TPM_UNDEFINED
, /* 195 */
285 TPM_MEDIUM
, /* 210 */
290 TPM_UNDEFINED
, /* 215 */
300 TPM_UNDEFINED
, /* 225 */
310 TPM_UNDEFINED
, /* 235 */
320 static void user_reader_timeout(unsigned long ptr
)
322 struct tpm_chip
*chip
= (struct tpm_chip
*) ptr
;
324 schedule_work(&chip
->work
);
327 static void timeout_work(struct work_struct
*work
)
329 struct tpm_chip
*chip
= container_of(work
, struct tpm_chip
, work
);
331 down(&chip
->buffer_mutex
);
332 atomic_set(&chip
->data_pending
, 0);
333 memset(chip
->data_buffer
, 0, TPM_BUFSIZE
);
334 up(&chip
->buffer_mutex
);
338 * Returns max number of jiffies to wait
340 unsigned long tpm_calc_ordinal_duration(struct tpm_chip
*chip
,
343 int duration_idx
= TPM_UNDEFINED
;
346 if (ordinal
< TPM_MAX_ORDINAL
)
347 duration_idx
= tpm_ordinal_duration
[ordinal
];
348 else if ((ordinal
& TPM_PROTECTED_ORDINAL_MASK
) <
349 TPM_MAX_PROTECTED_ORDINAL
)
351 tpm_protected_ordinal_duration
[ordinal
&
352 TPM_PROTECTED_ORDINAL_MASK
];
354 if (duration_idx
!= TPM_UNDEFINED
)
355 duration
= chip
->vendor
.duration
[duration_idx
];
361 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration
);
364 * Internal kernel interface to transmit TPM commands
366 static ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
373 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
374 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
377 if (count
> bufsiz
) {
379 "invalid count value %x %zx \n", count
, bufsiz
);
383 down(&chip
->tpm_mutex
);
385 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
387 "tpm_transmit: tpm_send: error %zd\n", rc
);
391 if (chip
->vendor
.irq
)
394 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
396 u8 status
= chip
->vendor
.status(chip
);
397 if ((status
& chip
->vendor
.req_complete_mask
) ==
398 chip
->vendor
.req_complete_val
)
401 if ((status
== chip
->vendor
.req_canceled
)) {
402 dev_err(chip
->dev
, "Operation Canceled\n");
407 msleep(TPM_TIMEOUT
); /* CHECK */
409 } while (time_before(jiffies
, stop
));
411 chip
->vendor
.cancel(chip
);
412 dev_err(chip
->dev
, "Operation Timed out\n");
417 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
420 "tpm_transmit: tpm_recv: error %zd\n", rc
);
422 up(&chip
->tpm_mutex
);
426 #define TPM_DIGEST_SIZE 20
427 #define TPM_ERROR_SIZE 10
428 #define TPM_RET_CODE_IDX 6
429 #define TPM_GET_CAP_RET_SIZE_IDX 10
430 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
431 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
432 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
433 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
434 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
435 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
436 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
437 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
439 #define TPM_CAP_IDX 13
440 #define TPM_CAP_SUBCAP_IDX 21
442 enum tpm_capabilities
{
447 enum tpm_sub_capabilities
{
448 TPM_CAP_PROP_PCR
= 0x1,
449 TPM_CAP_PROP_MANUFACTURER
= 0x3,
450 TPM_CAP_FLAG_PERM
= 0x8,
451 TPM_CAP_FLAG_VOL
= 0x9,
452 TPM_CAP_PROP_OWNER
= 0x11,
453 TPM_CAP_PROP_TIS_TIMEOUT
= 0x15,
454 TPM_CAP_PROP_TIS_DURATION
= 0x20,
458 * This is a semi generic GetCapability command for use
459 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
460 * and their associated sub_capabilities.
463 static const u8 tpm_cap
[] = {
464 0, 193, /* TPM_TAG_RQU_COMMAND */
465 0, 0, 0, 22, /* length */
466 0, 0, 0, 101, /* TPM_ORD_GetCapability */
467 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
468 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
469 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
472 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, u8
*data
, int len
,
477 len
= tpm_transmit(chip
, data
, len
);
480 if (len
== TPM_ERROR_SIZE
) {
481 err
= be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
)));
482 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
488 void tpm_gen_interrupt(struct tpm_chip
*chip
)
490 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
493 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
494 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
495 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
497 rc
= transmit_cmd(chip
, data
, sizeof(data
),
498 "attempting to determine the timeouts");
500 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
502 void tpm_get_timeouts(struct tpm_chip
*chip
)
504 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
508 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
509 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
510 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
512 rc
= transmit_cmd(chip
, data
, sizeof(data
),
513 "attempting to determine the timeouts");
517 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
521 /* Don't overwrite default if value is 0 */
523 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
)));
525 chip
->vendor
.timeout_a
= msecs_to_jiffies(timeout
);
527 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_2_IDX
)));
529 chip
->vendor
.timeout_b
= msecs_to_jiffies(timeout
);
531 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_3_IDX
)));
533 chip
->vendor
.timeout_c
= msecs_to_jiffies(timeout
);
535 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_4_IDX
)));
537 chip
->vendor
.timeout_d
= msecs_to_jiffies(timeout
);
540 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
541 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
542 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_DURATION
;
544 rc
= transmit_cmd(chip
, data
, sizeof(data
),
545 "attempting to determine the durations");
549 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
553 chip
->vendor
.duration
[TPM_SHORT
] =
554 msecs_to_jiffies(be32_to_cpu
555 (*((__be32
*) (data
+
556 TPM_GET_CAP_RET_UINT32_1_IDX
))));
557 chip
->vendor
.duration
[TPM_MEDIUM
] =
558 msecs_to_jiffies(be32_to_cpu
559 (*((__be32
*) (data
+
560 TPM_GET_CAP_RET_UINT32_2_IDX
))));
561 chip
->vendor
.duration
[TPM_LONG
] =
562 msecs_to_jiffies(be32_to_cpu
563 (*((__be32
*) (data
+
564 TPM_GET_CAP_RET_UINT32_3_IDX
))));
566 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
568 void tpm_continue_selftest(struct tpm_chip
*chip
)
571 0, 193, /* TPM_TAG_RQU_COMMAND */
572 0, 0, 0, 10, /* length */
573 0, 0, 0, 83, /* TPM_ORD_GetCapability */
576 tpm_transmit(chip
, data
, sizeof(data
));
578 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
580 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
583 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
586 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
590 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
591 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
592 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
594 rc
= transmit_cmd(chip
, data
, sizeof(data
),
595 "attemtping to determine the permanent state");
598 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_DISABLE_IDX
]);
600 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
602 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
605 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
608 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
612 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
613 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
614 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
616 rc
= transmit_cmd(chip
, data
, sizeof(data
),
617 "attemtping to determine the permanent state");
620 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_INACTIVE_IDX
]);
622 EXPORT_SYMBOL_GPL(tpm_show_active
);
624 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
627 u8 data
[sizeof(tpm_cap
)];
630 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
634 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
635 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
636 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_OWNER
;
638 rc
= transmit_cmd(chip
, data
, sizeof(data
),
639 "attempting to determine the owner state");
642 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_RET_BOOL_1_IDX
]);
644 EXPORT_SYMBOL_GPL(tpm_show_owned
);
646 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
647 struct device_attribute
* attr
, char *buf
)
649 u8 data
[sizeof(tpm_cap
)];
652 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
656 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
657 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
658 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_VOL
;
660 rc
= transmit_cmd(chip
, data
, sizeof(data
),
661 "attempting to determine the temporary state");
664 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_TEMP_INACTIVE_IDX
]);
666 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
668 static const u8 pcrread
[] = {
669 0, 193, /* TPM_TAG_RQU_COMMAND */
670 0, 0, 0, 14, /* length */
671 0, 0, 0, 21, /* TPM_ORD_PcrRead */
672 0, 0, 0, 0 /* PCR index */
675 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
678 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(pcrread
)), 30)];
684 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
688 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
689 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
690 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_PCR
;
692 rc
= transmit_cmd(chip
, data
, sizeof(data
),
693 "attempting to determine the number of PCRS");
697 num_pcrs
= be32_to_cpu(*((__be32
*) (data
+ 14)));
698 for (i
= 0; i
< num_pcrs
; i
++) {
699 memcpy(data
, pcrread
, sizeof(pcrread
));
700 index
= cpu_to_be32(i
);
701 memcpy(data
+ 10, &index
, 4);
702 rc
= transmit_cmd(chip
, data
, sizeof(data
),
703 "attempting to read a PCR");
706 str
+= sprintf(str
, "PCR-%02d: ", i
);
707 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
708 str
+= sprintf(str
, "%02X ", *(data
+ 10 + j
));
709 str
+= sprintf(str
, "\n");
714 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
716 #define READ_PUBEK_RESULT_SIZE 314
717 static const u8 readpubek
[] = {
718 0, 193, /* TPM_TAG_RQU_COMMAND */
719 0, 0, 0, 30, /* length */
720 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
723 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
731 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
735 data
= kzalloc(READ_PUBEK_RESULT_SIZE
, GFP_KERNEL
);
739 memcpy(data
, readpubek
, sizeof(readpubek
));
741 err
= transmit_cmd(chip
, data
, READ_PUBEK_RESULT_SIZE
,
742 "attempting to read the PUBEK");
747 ignore header 10 bytes
748 algorithm 32 bits (1 == RSA )
751 parameters (RSA 12->bytes: keybit, #primes, expbit)
754 ignore checksum 20 bytes
759 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
760 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
761 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
762 "Modulus length: %d\nModulus: \n",
763 data
[10], data
[11], data
[12], data
[13], data
[14],
764 data
[15], data
[16], data
[17], data
[22], data
[23],
765 data
[24], data
[25], data
[26], data
[27], data
[28],
766 data
[29], data
[30], data
[31], data
[32], data
[33],
767 be32_to_cpu(*((__be32
*) (data
+ 34))));
769 for (i
= 0; i
< 256; i
++) {
770 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
771 if ((i
+ 1) % 16 == 0)
772 str
+= sprintf(str
, "\n");
779 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
781 #define CAP_VERSION_1_1 6
782 #define CAP_VERSION_1_2 0x1A
783 #define CAP_VERSION_IDX 13
784 static const u8 cap_version
[] = {
785 0, 193, /* TPM_TAG_RQU_COMMAND */
786 0, 0, 0, 18, /* length */
787 0, 0, 0, 101, /* TPM_ORD_GetCapability */
792 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
795 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
799 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
803 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
804 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
805 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
807 rc
= transmit_cmd(chip
, data
, sizeof(data
),
808 "attempting to determine the manufacturer");
812 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
813 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
815 memcpy(data
, cap_version
, sizeof(cap_version
));
816 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_1
;
817 rc
= transmit_cmd(chip
, data
, sizeof(data
),
818 "attempting to determine the 1.1 version");
823 "TCG version: %d.%d\nFirmware version: %d.%d\n",
824 (int) data
[14], (int) data
[15], (int) data
[16],
830 EXPORT_SYMBOL_GPL(tpm_show_caps
);
832 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
833 struct device_attribute
* attr
, char *buf
)
835 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
839 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
843 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
844 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
845 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
847 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
849 dev_dbg(chip
->dev
, "A TPM error (%d) occurred "
850 "attempting to determine the manufacturer\n",
851 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
855 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
856 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
858 memcpy(data
, cap_version
, sizeof(cap_version
));
859 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_2
;
861 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
863 dev_err(chip
->dev
, "A TPM error (%d) occurred "
864 "attempting to determine the 1.2 version\n",
865 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
869 "TCG version: %d.%d\nFirmware version: %d.%d\n",
870 (int) data
[16], (int) data
[17], (int) data
[18],
876 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
878 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
879 const char *buf
, size_t count
)
881 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
885 chip
->vendor
.cancel(chip
);
888 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
891 * Device file system interface to the TPM
893 int tpm_open(struct inode
*inode
, struct file
*file
)
895 int rc
= 0, minor
= iminor(inode
);
896 struct tpm_chip
*chip
= NULL
, *pos
;
898 spin_lock(&driver_lock
);
900 list_for_each_entry(pos
, &tpm_chip_list
, list
) {
901 if (pos
->vendor
.miscdev
.minor
== minor
) {
912 if (chip
->num_opens
) {
913 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
919 get_device(chip
->dev
);
921 spin_unlock(&driver_lock
);
923 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
924 if (chip
->data_buffer
== NULL
) {
926 put_device(chip
->dev
);
930 atomic_set(&chip
->data_pending
, 0);
932 file
->private_data
= chip
;
936 spin_unlock(&driver_lock
);
939 EXPORT_SYMBOL_GPL(tpm_open
);
941 int tpm_release(struct inode
*inode
, struct file
*file
)
943 struct tpm_chip
*chip
= file
->private_data
;
945 spin_lock(&driver_lock
);
946 file
->private_data
= NULL
;
948 del_singleshot_timer_sync(&chip
->user_read_timer
);
949 flush_scheduled_work();
950 atomic_set(&chip
->data_pending
, 0);
951 put_device(chip
->dev
);
952 kfree(chip
->data_buffer
);
953 spin_unlock(&driver_lock
);
956 EXPORT_SYMBOL_GPL(tpm_release
);
958 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
959 size_t size
, loff_t
*off
)
961 struct tpm_chip
*chip
= file
->private_data
;
962 int in_size
= size
, out_size
;
964 /* cannot perform a write until the read has cleared
965 either via tpm_read or a user_read_timer timeout */
966 while (atomic_read(&chip
->data_pending
) != 0)
969 down(&chip
->buffer_mutex
);
971 if (in_size
> TPM_BUFSIZE
)
972 in_size
= TPM_BUFSIZE
;
975 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
976 up(&chip
->buffer_mutex
);
980 /* atomic tpm command send and result receive */
981 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
983 atomic_set(&chip
->data_pending
, out_size
);
984 up(&chip
->buffer_mutex
);
986 /* Set a timeout by which the reader must come claim the result */
987 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
991 EXPORT_SYMBOL_GPL(tpm_write
);
993 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
994 size_t size
, loff_t
*off
)
996 struct tpm_chip
*chip
= file
->private_data
;
999 del_singleshot_timer_sync(&chip
->user_read_timer
);
1000 flush_scheduled_work();
1001 ret_size
= atomic_read(&chip
->data_pending
);
1002 atomic_set(&chip
->data_pending
, 0);
1003 if (ret_size
> 0) { /* relay data */
1004 if (size
< ret_size
)
1007 down(&chip
->buffer_mutex
);
1008 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1010 up(&chip
->buffer_mutex
);
1015 EXPORT_SYMBOL_GPL(tpm_read
);
1017 void tpm_remove_hardware(struct device
*dev
)
1019 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1022 dev_err(dev
, "No device data found\n");
1026 spin_lock(&driver_lock
);
1028 list_del(&chip
->list
);
1030 spin_unlock(&driver_lock
);
1032 dev_set_drvdata(dev
, NULL
);
1033 misc_deregister(&chip
->vendor
.miscdev
);
1034 kfree(chip
->vendor
.miscdev
.name
);
1036 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1037 tpm_bios_log_teardown(chip
->bios_dir
);
1039 clear_bit(chip
->dev_num
, dev_mask
);
1045 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1047 static u8 savestate
[] = {
1048 0, 193, /* TPM_TAG_RQU_COMMAND */
1049 0, 0, 0, 10, /* blob length (in bytes) */
1050 0, 0, 0, 152 /* TPM_ORD_SaveState */
1054 * We are about to suspend. Save the TPM state
1055 * so that it can be restored.
1057 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1059 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1063 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1066 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1069 * Resume from a power safe. The BIOS already restored
1072 int tpm_pm_resume(struct device
*dev
)
1074 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1081 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1084 * Called from tpm_<specific>.c probe function only for devices
1085 * the driver has determined it should claim. Prior to calling
1086 * this function the specific probe function has called pci_enable_device
1087 * upon errant exit from this function specific probe function should call
1088 * pci_disable_device
1090 struct tpm_chip
*tpm_register_hardware(struct device
*dev
, const struct tpm_vendor_specific
1093 #define DEVNAME_SIZE 7
1096 struct tpm_chip
*chip
;
1098 /* Driver specific per-device data */
1099 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1103 init_MUTEX(&chip
->buffer_mutex
);
1104 init_MUTEX(&chip
->tpm_mutex
);
1105 INIT_LIST_HEAD(&chip
->list
);
1107 INIT_WORK(&chip
->work
, timeout_work
);
1109 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1110 (unsigned long)chip
);
1112 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1114 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1116 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1117 dev_err(dev
, "No available tpm device numbers\n");
1120 } else if (chip
->dev_num
== 0)
1121 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1123 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1125 set_bit(chip
->dev_num
, dev_mask
);
1127 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1128 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1129 chip
->vendor
.miscdev
.name
= devname
;
1131 chip
->vendor
.miscdev
.parent
= dev
;
1132 chip
->dev
= get_device(dev
);
1134 if (misc_register(&chip
->vendor
.miscdev
)) {
1136 "unable to misc_register %s, minor %d\n",
1137 chip
->vendor
.miscdev
.name
,
1138 chip
->vendor
.miscdev
.minor
);
1140 clear_bit(chip
->dev_num
, dev_mask
);
1146 spin_lock(&driver_lock
);
1148 dev_set_drvdata(dev
, chip
);
1150 list_add(&chip
->list
, &tpm_chip_list
);
1152 spin_unlock(&driver_lock
);
1154 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1155 list_del(&chip
->list
);
1156 misc_deregister(&chip
->vendor
.miscdev
);
1158 clear_bit(chip
->dev_num
, dev_mask
);
1164 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1168 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1170 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1171 MODULE_DESCRIPTION("TPM Driver");
1172 MODULE_VERSION("2.0");
1173 MODULE_LICENSE("GPL");