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,
35 TPM_NUM_MASK_ENTRIES
= TPM_NUM_DEVICES
/ (8 * sizeof(int))
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 int dev_mask
[TPM_NUM_MASK_ENTRIES
];
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(void *ptr
)
331 struct tpm_chip
*chip
= ptr
;
333 down(&chip
->buffer_mutex
);
334 atomic_set(&chip
->data_pending
, 0);
335 memset(chip
->data_buffer
, 0, TPM_BUFSIZE
);
336 up(&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
] * HZ
/ 1000;
363 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration
);
366 * Internal kernel interface to transmit TPM commands
368 static ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
375 count
= be32_to_cpu(*((__be32
*) (buf
+ 2)));
376 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
379 if (count
> bufsiz
) {
381 "invalid count value %x %zx \n", count
, bufsiz
);
385 down(&chip
->tpm_mutex
);
387 if ((rc
= chip
->vendor
.send(chip
, (u8
*) buf
, count
)) < 0) {
389 "tpm_transmit: tpm_send: error %zd\n", rc
);
393 if (chip
->vendor
.irq
)
396 stop
= jiffies
+ tpm_calc_ordinal_duration(chip
, ordinal
);
398 u8 status
= chip
->vendor
.status(chip
);
399 if ((status
& chip
->vendor
.req_complete_mask
) ==
400 chip
->vendor
.req_complete_val
)
403 if ((status
== chip
->vendor
.req_canceled
)) {
404 dev_err(chip
->dev
, "Operation Canceled\n");
409 msleep(TPM_TIMEOUT
); /* CHECK */
411 } while (time_before(jiffies
, stop
));
413 chip
->vendor
.cancel(chip
);
414 dev_err(chip
->dev
, "Operation Timed out\n");
419 rc
= chip
->vendor
.recv(chip
, (u8
*) buf
, bufsiz
);
422 "tpm_transmit: tpm_recv: error %zd\n", rc
);
424 up(&chip
->tpm_mutex
);
428 #define TPM_DIGEST_SIZE 20
429 #define TPM_ERROR_SIZE 10
430 #define TPM_RET_CODE_IDX 6
431 #define TPM_GET_CAP_RET_SIZE_IDX 10
432 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
433 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
434 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
435 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
436 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
437 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
438 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
439 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
441 #define TPM_CAP_IDX 13
442 #define TPM_CAP_SUBCAP_IDX 21
444 enum tpm_capabilities
{
449 enum tpm_sub_capabilities
{
450 TPM_CAP_PROP_PCR
= 0x1,
451 TPM_CAP_PROP_MANUFACTURER
= 0x3,
452 TPM_CAP_FLAG_PERM
= 0x8,
453 TPM_CAP_FLAG_VOL
= 0x9,
454 TPM_CAP_PROP_OWNER
= 0x11,
455 TPM_CAP_PROP_TIS_TIMEOUT
= 0x15,
456 TPM_CAP_PROP_TIS_DURATION
= 0x20,
460 * This is a semi generic GetCapability command for use
461 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
462 * and their associated sub_capabilities.
465 static const u8 tpm_cap
[] = {
466 0, 193, /* TPM_TAG_RQU_COMMAND */
467 0, 0, 0, 22, /* length */
468 0, 0, 0, 101, /* TPM_ORD_GetCapability */
469 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
470 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
471 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
474 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, u8
*data
, int len
,
479 len
= tpm_transmit(chip
, data
, len
);
482 if (len
== TPM_ERROR_SIZE
) {
483 err
= be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
)));
484 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
490 void tpm_gen_interrupt(struct tpm_chip
*chip
)
492 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
495 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
496 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
497 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
499 rc
= transmit_cmd(chip
, data
, sizeof(data
),
500 "attempting to determine the timeouts");
502 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
504 void tpm_get_timeouts(struct tpm_chip
*chip
)
506 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 30)];
510 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
511 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
512 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_TIMEOUT
;
514 rc
= transmit_cmd(chip
, data
, sizeof(data
),
515 "attempting to determine the timeouts");
519 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
523 /* Don't overwrite default if value is 0 */
525 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
)));
527 chip
->vendor
.timeout_a
= timeout
;
529 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_2_IDX
)));
531 chip
->vendor
.timeout_b
= timeout
;
533 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_3_IDX
)));
535 chip
->vendor
.timeout_c
= timeout
;
537 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_4_IDX
)));
539 chip
->vendor
.timeout_d
= timeout
;
542 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
543 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
544 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_TIS_DURATION
;
546 rc
= transmit_cmd(chip
, data
, sizeof(data
),
547 "attempting to determine the durations");
551 if (be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_SIZE_IDX
)))
555 chip
->vendor
.duration
[TPM_SHORT
] =
556 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
)));
557 chip
->vendor
.duration
[TPM_MEDIUM
] =
558 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_2_IDX
)));
559 chip
->vendor
.duration
[TPM_LONG
] =
560 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_3_IDX
)));
562 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
564 void tpm_continue_selftest(struct tpm_chip
*chip
)
567 0, 193, /* TPM_TAG_RQU_COMMAND */
568 0, 0, 0, 10, /* length */
569 0, 0, 0, 83, /* TPM_ORD_GetCapability */
572 tpm_transmit(chip
, data
, sizeof(data
));
574 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
576 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
579 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
582 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
586 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
587 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
588 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
590 rc
= transmit_cmd(chip
, data
, sizeof(data
),
591 "attemtping to determine the permanent state");
594 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_DISABLE_IDX
]);
596 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
598 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
601 u8 data
[max_t(int, ARRAY_SIZE(tpm_cap
), 35)];
604 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
608 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
609 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
610 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_PERM
;
612 rc
= transmit_cmd(chip
, data
, sizeof(data
),
613 "attemtping to determine the permanent state");
616 return sprintf(buf
, "%d\n", !data
[TPM_GET_CAP_PERM_INACTIVE_IDX
]);
618 EXPORT_SYMBOL_GPL(tpm_show_active
);
620 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
623 u8 data
[sizeof(tpm_cap
)];
626 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
630 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
631 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
632 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_OWNER
;
634 rc
= transmit_cmd(chip
, data
, sizeof(data
),
635 "attempting to determine the owner state");
638 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_RET_BOOL_1_IDX
]);
640 EXPORT_SYMBOL_GPL(tpm_show_owned
);
642 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
643 struct device_attribute
* attr
, char *buf
)
645 u8 data
[sizeof(tpm_cap
)];
648 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
652 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
653 data
[TPM_CAP_IDX
] = TPM_CAP_FLAG
;
654 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_FLAG_VOL
;
656 rc
= transmit_cmd(chip
, data
, sizeof(data
),
657 "attempting to determine the temporary state");
660 return sprintf(buf
, "%d\n", data
[TPM_GET_CAP_TEMP_INACTIVE_IDX
]);
662 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
664 static const u8 pcrread
[] = {
665 0, 193, /* TPM_TAG_RQU_COMMAND */
666 0, 0, 0, 14, /* length */
667 0, 0, 0, 21, /* TPM_ORD_PcrRead */
668 0, 0, 0, 0 /* PCR index */
671 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
674 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(pcrread
)), 30)];
680 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
684 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
685 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
686 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_PCR
;
688 rc
= transmit_cmd(chip
, data
, sizeof(data
),
689 "attempting to determine the number of PCRS");
693 num_pcrs
= be32_to_cpu(*((__be32
*) (data
+ 14)));
694 for (i
= 0; i
< num_pcrs
; i
++) {
695 memcpy(data
, pcrread
, sizeof(pcrread
));
696 index
= cpu_to_be32(i
);
697 memcpy(data
+ 10, &index
, 4);
698 rc
= transmit_cmd(chip
, data
, sizeof(data
),
699 "attempting to read a PCR");
702 str
+= sprintf(str
, "PCR-%02d: ", i
);
703 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
704 str
+= sprintf(str
, "%02X ", *(data
+ 10 + j
));
705 str
+= sprintf(str
, "\n");
710 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
712 #define READ_PUBEK_RESULT_SIZE 314
713 static const u8 readpubek
[] = {
714 0, 193, /* TPM_TAG_RQU_COMMAND */
715 0, 0, 0, 30, /* length */
716 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
719 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
727 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
731 data
= kzalloc(READ_PUBEK_RESULT_SIZE
, GFP_KERNEL
);
735 memcpy(data
, readpubek
, sizeof(readpubek
));
737 err
= transmit_cmd(chip
, data
, READ_PUBEK_RESULT_SIZE
,
738 "attempting to read the PUBEK");
743 ignore header 10 bytes
744 algorithm 32 bits (1 == RSA )
747 parameters (RSA 12->bytes: keybit, #primes, expbit)
750 ignore checksum 20 bytes
755 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
756 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
757 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
758 "Modulus length: %d\nModulus: \n",
759 data
[10], data
[11], data
[12], data
[13], data
[14],
760 data
[15], data
[16], data
[17], data
[22], data
[23],
761 data
[24], data
[25], data
[26], data
[27], data
[28],
762 data
[29], data
[30], data
[31], data
[32], data
[33],
763 be32_to_cpu(*((__be32
*) (data
+ 34))));
765 for (i
= 0; i
< 256; i
++) {
766 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
767 if ((i
+ 1) % 16 == 0)
768 str
+= sprintf(str
, "\n");
775 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
777 #define CAP_VERSION_1_1 6
778 #define CAP_VERSION_1_2 0x1A
779 #define CAP_VERSION_IDX 13
780 static const u8 cap_version
[] = {
781 0, 193, /* TPM_TAG_RQU_COMMAND */
782 0, 0, 0, 18, /* length */
783 0, 0, 0, 101, /* TPM_ORD_GetCapability */
788 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
791 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
795 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
799 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
800 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
801 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
803 rc
= transmit_cmd(chip
, data
, sizeof(data
),
804 "attempting to determine the manufacturer");
808 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
809 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
811 memcpy(data
, cap_version
, sizeof(cap_version
));
812 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_1
;
813 rc
= transmit_cmd(chip
, data
, sizeof(data
),
814 "attempting to determine the 1.1 version");
819 "TCG version: %d.%d\nFirmware version: %d.%d\n",
820 (int) data
[14], (int) data
[15], (int) data
[16],
826 EXPORT_SYMBOL_GPL(tpm_show_caps
);
828 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
829 struct device_attribute
* attr
, char *buf
)
831 u8 data
[max_t(int, max(ARRAY_SIZE(tpm_cap
), ARRAY_SIZE(cap_version
)), 30)];
835 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
839 memcpy(data
, tpm_cap
, sizeof(tpm_cap
));
840 data
[TPM_CAP_IDX
] = TPM_CAP_PROP
;
841 data
[TPM_CAP_SUBCAP_IDX
] = TPM_CAP_PROP_MANUFACTURER
;
843 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
845 dev_dbg(chip
->dev
, "A TPM error (%d) occurred "
846 "attempting to determine the manufacturer\n",
847 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
851 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
852 be32_to_cpu(*((__be32
*) (data
+ TPM_GET_CAP_RET_UINT32_1_IDX
))));
854 memcpy(data
, cap_version
, sizeof(cap_version
));
855 data
[CAP_VERSION_IDX
] = CAP_VERSION_1_2
;
857 if ((len
= tpm_transmit(chip
, data
, sizeof(data
))) <=
859 dev_err(chip
->dev
, "A TPM error (%d) occurred "
860 "attempting to determine the 1.2 version\n",
861 be32_to_cpu(*((__be32
*) (data
+ TPM_RET_CODE_IDX
))));
865 "TCG version: %d.%d\nFirmware version: %d.%d\n",
866 (int) data
[16], (int) data
[17], (int) data
[18],
872 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
874 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
875 const char *buf
, size_t count
)
877 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
881 chip
->vendor
.cancel(chip
);
884 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
887 * Device file system interface to the TPM
889 int tpm_open(struct inode
*inode
, struct file
*file
)
891 int rc
= 0, minor
= iminor(inode
);
892 struct tpm_chip
*chip
= NULL
, *pos
;
894 spin_lock(&driver_lock
);
896 list_for_each_entry(pos
, &tpm_chip_list
, list
) {
897 if (pos
->vendor
.miscdev
.minor
== minor
) {
908 if (chip
->num_opens
) {
909 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
915 get_device(chip
->dev
);
917 spin_unlock(&driver_lock
);
919 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
920 if (chip
->data_buffer
== NULL
) {
922 put_device(chip
->dev
);
926 atomic_set(&chip
->data_pending
, 0);
928 file
->private_data
= chip
;
932 spin_unlock(&driver_lock
);
935 EXPORT_SYMBOL_GPL(tpm_open
);
937 int tpm_release(struct inode
*inode
, struct file
*file
)
939 struct tpm_chip
*chip
= file
->private_data
;
941 spin_lock(&driver_lock
);
942 file
->private_data
= NULL
;
944 del_singleshot_timer_sync(&chip
->user_read_timer
);
945 flush_scheduled_work();
946 atomic_set(&chip
->data_pending
, 0);
947 put_device(chip
->dev
);
948 kfree(chip
->data_buffer
);
949 spin_unlock(&driver_lock
);
952 EXPORT_SYMBOL_GPL(tpm_release
);
954 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
955 size_t size
, loff_t
*off
)
957 struct tpm_chip
*chip
= file
->private_data
;
958 int in_size
= size
, out_size
;
960 /* cannot perform a write until the read has cleared
961 either via tpm_read or a user_read_timer timeout */
962 while (atomic_read(&chip
->data_pending
) != 0)
965 down(&chip
->buffer_mutex
);
967 if (in_size
> TPM_BUFSIZE
)
968 in_size
= TPM_BUFSIZE
;
971 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
972 up(&chip
->buffer_mutex
);
976 /* atomic tpm command send and result receive */
977 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
979 atomic_set(&chip
->data_pending
, out_size
);
980 up(&chip
->buffer_mutex
);
982 /* Set a timeout by which the reader must come claim the result */
983 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
987 EXPORT_SYMBOL_GPL(tpm_write
);
989 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
990 size_t size
, loff_t
*off
)
992 struct tpm_chip
*chip
= file
->private_data
;
995 del_singleshot_timer_sync(&chip
->user_read_timer
);
996 flush_scheduled_work();
997 ret_size
= atomic_read(&chip
->data_pending
);
998 atomic_set(&chip
->data_pending
, 0);
999 if (ret_size
> 0) { /* relay data */
1000 if (size
< ret_size
)
1003 down(&chip
->buffer_mutex
);
1004 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1006 up(&chip
->buffer_mutex
);
1011 EXPORT_SYMBOL_GPL(tpm_read
);
1013 void tpm_remove_hardware(struct device
*dev
)
1015 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1018 dev_err(dev
, "No device data found\n");
1022 spin_lock(&driver_lock
);
1024 list_del(&chip
->list
);
1026 spin_unlock(&driver_lock
);
1028 dev_set_drvdata(dev
, NULL
);
1029 misc_deregister(&chip
->vendor
.miscdev
);
1030 kfree(chip
->vendor
.miscdev
.name
);
1032 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1033 tpm_bios_log_teardown(chip
->bios_dir
);
1035 dev_mask
[chip
->dev_num
/ TPM_NUM_MASK_ENTRIES
] &=
1036 ~(1 << (chip
->dev_num
% TPM_NUM_MASK_ENTRIES
));
1042 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1044 static u8 savestate
[] = {
1045 0, 193, /* TPM_TAG_RQU_COMMAND */
1046 0, 0, 0, 10, /* blob length (in bytes) */
1047 0, 0, 0, 152 /* TPM_ORD_SaveState */
1051 * We are about to suspend. Save the TPM state
1052 * so that it can be restored.
1054 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1056 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1060 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1063 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1066 * Resume from a power safe. The BIOS already restored
1069 int tpm_pm_resume(struct device
*dev
)
1071 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1078 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1081 * Called from tpm_<specific>.c probe function only for devices
1082 * the driver has determined it should claim. Prior to calling
1083 * this function the specific probe function has called pci_enable_device
1084 * upon errant exit from this function specific probe function should call
1085 * pci_disable_device
1087 struct tpm_chip
*tpm_register_hardware(struct device
*dev
, const struct tpm_vendor_specific
1090 #define DEVNAME_SIZE 7
1093 struct tpm_chip
*chip
;
1096 /* Driver specific per-device data */
1097 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1101 init_MUTEX(&chip
->buffer_mutex
);
1102 init_MUTEX(&chip
->tpm_mutex
);
1103 INIT_LIST_HEAD(&chip
->list
);
1105 INIT_WORK(&chip
->work
, timeout_work
, chip
);
1107 init_timer(&chip
->user_read_timer
);
1108 chip
->user_read_timer
.function
= user_reader_timeout
;
1109 chip
->user_read_timer
.data
= (unsigned long) chip
;
1111 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1115 for (i
= 0; i
< TPM_NUM_MASK_ENTRIES
; i
++)
1116 for (j
= 0; j
< 8 * sizeof(int); j
++)
1117 if ((dev_mask
[i
] & (1 << j
)) == 0) {
1119 i
* TPM_NUM_MASK_ENTRIES
+ j
;
1120 dev_mask
[i
] |= 1 << j
;
1121 goto dev_num_search_complete
;
1124 dev_num_search_complete
:
1125 if (chip
->dev_num
< 0) {
1126 dev_err(dev
, "No available tpm device numbers\n");
1129 } else if (chip
->dev_num
== 0)
1130 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1132 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1134 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1135 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1136 chip
->vendor
.miscdev
.name
= devname
;
1138 chip
->vendor
.miscdev
.dev
= dev
;
1139 chip
->dev
= get_device(dev
);
1141 if (misc_register(&chip
->vendor
.miscdev
)) {
1143 "unable to misc_register %s, minor %d\n",
1144 chip
->vendor
.miscdev
.name
,
1145 chip
->vendor
.miscdev
.minor
);
1148 dev_mask
[i
] &= !(1 << j
);
1152 spin_lock(&driver_lock
);
1154 dev_set_drvdata(dev
, chip
);
1156 list_add(&chip
->list
, &tpm_chip_list
);
1158 spin_unlock(&driver_lock
);
1160 sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1162 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1166 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1168 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1169 MODULE_DESCRIPTION("TPM Driver");
1170 MODULE_VERSION("2.0");
1171 MODULE_LICENSE("GPL");